redis 应用场景

时间:2023-05-12 14:21:02

1.string类型 : 图片和视频文件,静态文件

2.list 双向链表:回帖ID,我的关注列表,消息队列

length = redis.lpush('users:newest', 'user:goku')
if length >
#trim is to we only keep "newest" users
redis.rpop('users:newest')
end

最新注册用户 $newest = [ 'goku', 'tom', ...];

# get the  newest users
keys = redis.lrange('users:newest', , )
#multi get the actual user objects
redis.mget(*keys)

3.set无序集合,通过hash table实现,优点是快速查找元素是否存在 :

  a. 记录一些不能重复的记录,例如用户名

  b. 记录做过的事情,比如限制用户一天内投一票,用时间做key,用户ID做member,以时间key来查询用户ID是否在members里面来确认是否投票。

Sets are the kind of data structure you use to keep track of friends and tags:

SADD friends:leto ghanima
SADD friends:leto duncan
SADD friends:paul duncan
SADD friends:paul gurney
SINTER friends:leto friends:paul
) "duncan"

4. sorted set 有序集合,用double类型的整数进行排序,由skipList(跳跃表)与Hash Table组合完成。

  a. 构建具有优先级的队列

  b. 排行榜排序,将排序的值设为score值

we could add a weight to our data:

ZADD friends:leto  ghanima
ZADD friends:leto duncan
ZADD friends:leto farad'n
ZRANGEBYSCORE friends:leto
) "duncan"
) "ghanima"

The above will get all of leto's friend with a score of 500-1000.

5. Hash类型,每个KEY对应一个Hash Table:适合用于存储对象,用户ID为KEY,用户数据为VALUE

HSET users:goku race sayan
HSET users:goku power
$user = new stdObj;
$user->race = ;
$user->power = ;

参考:

http://www.iyunv.com/thread-52670-1-1.html

REDIS处理

session.save_handler = redis

//多节点
session.save_path = "tcp://ip:port?auth=secret?weight=1&timeout=2.5,tcp://ip2:port2?weight=2" //单个节点
session.save_path = "tcp://ip:port?auth=secret?weight=1&timeout=2.5" //socket 方式
session.save_path = "unix:///var/run/redis/redis.sock?persistent=1&weight=1&database=0
ip: Redis 节点的 IP。

port: Redis 节点的端口。

auth: 与 Redis 节点进行权限验证。

weight: 权重,上面的例子表示session数量,ip2节点 是 ip1节点的两倍。

timeout: Redis 连接超时时间。单位:秒。连接失败时,Session不可用(风险!)

persistent: 持久连接。

prefix: 前缀,默认是 "PHPREDIS_SESSION:"。

database: 选择哪个 Redis 数据库。取值:int。参见 Redis 配置 databases 。