1. redis的lru的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
robj *lookupKey(redisDb *db, robj *key) { dictEntry *de = dictFind(db->dict,key->ptr); if (de) { robj *val = dictGetVal(de); /* Update the access time for the ageing algorithm. * Don't do it if we have a saving child, as this will trigger * a copy on write madness. */ if (server.rdb_child_pid == -1 && server.aof_child_pid == -1) val->lru = server.lruclock; return val; } else { return NULL; } } |
其中:
1 |
val->lru = server.lruclock; |
这个赋值是怎么体现lru的工作机制的?
2. 按照下面的调用过程来看redis对每个命令的处理过程:
1 2 3 4 5 6 7 8 9 |
(gdb) bt #0 delCommand (c=0xb6cd7000) at db.c:235 #1 0x0805e018 in call (c=c@entry=0xb6cd7000, flags=flags@entry=7) at redis.c:1599 #2 0x080604b0 in processCommand (c=c@entry=0xb6cd7000) at redis.c:1774 #3 0x08068b47 in processInputBuffer (c=c@entry=0xb6cd7000) at networking.c:1013 #4 0x08068c59 in readQueryFromClient (el=0xb6c0a0d0, fd=5, privdata=0xb6cd7000, mask=1) at networking.c:1076 #5 0x08059d19 in aeProcessEvents (eventLoop=eventLoop@entry=0xb6c0a0d0, flags=flags@entry=3) at ae.c:382 #6 0x08059ffc in aeMain (eventLoop=0xb6c0a0d0) at ae.c:425 #7 0x08058dac in main (argc=1, argv=0xbfc82d54) at redis.c:2721 |
至于redis协议中的每条指定对应server端的那一个函数,不会看到一个if..else.. 或者 switch结构的, 从 redisCommandTable 中来找就足够了(当然,quit命令找不到,似乎也只有quit命令)
3. 今天还了解一下细节
1) key和value都是作为redis的object来存储的,相同的value是重复使用的
2) 过期机制是通过将要过期的key写到一个过期table中来做的,这个是和memcache不同的
4. redis是支持认证机制的,当然,这样会多一次请求,而且认证请求不能和其它请求一起打包执行的
5. 关于Redis的LRU:
Redis的LRU是不精确的,不是在allkey中LRU的,而是在allkey中随机选取 maxmemory-samples 来LRU的,这一点要考虑一下是否满足需求。
(了解一下概率的计算方法吧)
摘录: http://oldblog.antirez.com/post/redis-as-LRU-cache.html
maxmemory-samples number_of_samples
The last config option is used to tune the algorithms precision. In order to save memory Redis just adds a 22 bits field to every object for LRU. When we need to remove a key we sample N keys, and remove the one that was idle for longer time. For default three keys are sampled, that is a reasonable approximation of LRU in the long run, but you can get more precision at the cost of some more CPU time changing the number of keys to sample.
Redis 2.2 的配置文件关于LRU的相关说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
################################### LIMITS #################################### # Set the max number of connected clients at the same time. By default there # is no limit, and it's up to the number of file descriptors the Redis process # is able to open. The special value '0' means no limits. # Once the limit is reached Redis will close all the new connections sending # an error 'max number of clients reached'. # # maxclients 128 # Don't use more memory than the specified amount of bytes. # When the memory limit is reached Redis will try to remove keys with an # EXPIRE set. It will try to start freeing keys that are going to expire # in little time and preserve keys with a longer time to live. # Redis will also try to remove objects from free lists if possible. # # If all this fails, Redis will start to reply with errors to commands # that will use more memory, like SET, LPUSH, and so on, and will continue # to reply to most read-only commands like GET. # # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a # 'state' server or cache, not as a real DB. When Redis is used as a real # database the memory usage will grow over the weeks, it will be obvious if # it is going to use too much memory in the long run, and you'll have the time # to upgrade. With maxmemory after the limit is reached you'll start to get # errors for write operations, and this may even lead to DB inconsistency. # # maxmemory <bytes> # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached? You can select among five behavior: # # volatile-lru -> remove the key with an expire set using an LRU algorithm # allkeys-lru -> remove any key accordingly to the LRU algorithm # volatile-random -> remove a random key with an expire set # allkeys->random -> remove a random key, any key # volatile-ttl -> remove the key with the nearest expire time (minor TTL) # noeviction -> don't expire at all, just return an error on write operations # # Note: with all the kind of policies, Redis will return an error on write # operations, when there are not suitable keys for eviction. # # At the date of writing this commands are: set setnx setex append # incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd # sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby # zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby # getset mset msetnx exec sort # # The default is: # # maxmemory-policy volatile-lru # LRU and minimal TTL algorithms are not precise algorithms but approximated # algorithms (in order to save memory), so you can select as well the sample # size to check. For instance for default Redis will check three keys and # pick the one that was used less recently, you can change the sample size # using the following configuration directive. # # maxmemory-samples 3 |