Redis数据结构之string类型

95 阅读1分钟

字符串常用操作

  • SET key value
  • MSET key value [key value ...]
  • SETNX key value
  • GET key
  • MGET key
  • DEL key [key...]
  • EXPIRE key seconds

原子加减

  • INCR key
  • DECR key
  • INCRBY key increment
  • INCRBY key decrement

应用场景

  • 单值缓存
SET key value
get key 
  • 对象缓存
1) SET user:1 value(json格式数据)
2) MSET user:1:name zhuge user:1:balance 1888
   MGET user:1:name user:1:balance
  • 分布式锁
 SETNX product:10001 true     //返回1代表获取锁成功  返回0表示获取锁失败
 DEL product:10001      //执行完业务释放锁
  • 计数器
INCR article:readCount:{文章ID}
GET article:readCOunt:{文章ID}
  • Web集群session共享
spring sessiong + redis实现session共享
  • 分布式系统全局序列号
INCRY orderId 1000   //redis批量生成序列号提升性能