闅忕潃nosql椋庢疆鍏磋搗錛?/span>redis浣滀負褰撲腑涓涓鐪肩殑鏄庢槦錛屼篃瓚婃潵瓚婂鐨勮鍏蟲敞鍜屼嬌鐢紝鎴戝湪宸ヤ綔涓篃騫挎硾鐨勭敤鍒頒簡redis鏉ュ厖褰?/span>cache鍜?/span>key-value DB錛屼絾褰撳ぇ瀹跺彂鐜版暟鎹秺鏉ヨ秺澶氭椂錛屼笉紱佹湁浜涙媴蹇冿紝redis鑳芥拺鐨勪綇鍚楋紝铏界劧瀹樻柟宸茬粡鏈夋紓浜殑benchmark錛岃嚜宸變篃鍙互鍋氬仛鍘嬪姏嫻嬭瘯錛屼絾鏄湅鐪嬫簮鐮侊紝涔熸槸紜闂鏈鐩存帴鐨勫姙娉曚箣涓銆傛瘮濡傜洰鍓嶆垜浠紜鐨勪竴涓棶棰樻槸錛?/span>redis鏄浣曞垹闄よ繃鏈熸暟鎹殑錛?/span>
鐢ㄤ竴涓彲浠?/span>"find reference"鐨?/span>IDE,娌跨潃setex(Set the value and expiration of a key)鍛戒護涓紿ョ┒绔燂細
void setexCommand(redisClient *c) {
c->argv[3] = tryObjectEncoding(c->argv[3]);
setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
}
setGenericCommand鏄竴涓疄鐜?/span>set,setnx,setex鐨勯氱敤鍑芥暟錛屽弬鏁拌緗笉鍚岃屽凡銆?/span>
void setCommand(redisClient *c) {
c->argv[2] = tryObjectEncoding(c->argv[2]);
setGenericCommand(c,0,c->argv[1],c->argv[2],NULL);
}
void setnxCommand(redisClient *c) {
c->argv[2] = tryObjectEncoding(c->argv[2]);
setGenericCommand(c,1,c->argv[1],c->argv[2],NULL);
}
void setexCommand(redisClient *c) {
c->argv[3] = tryObjectEncoding(c->argv[3]);
setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
}
鍐嶇湅setGenericCommand錛?br />
1 void setGenericCommand(redisClient *c, int nx, robj *key, robj *val, robj *expire) {
2 long seconds = 0; /* initialized to avoid an harmness warning */
3
4 if (expire) {
5 if (getLongFromObjectOrReply(c, expire, &seconds, NULL) != REDIS_OK)
6 return;
7 if (seconds <= 0) {
8 addReplyError(c,"invalid expire time in SETEX");
9 return;
10 }
11 }
12
13 if (lookupKeyWrite(c->db,key) != NULL && nx) {
14 addReply(c,shared.czero);
15 return;
16 }
17 setKey(c->db,key,val);
18 server.dirty++;
19 if (expire) setExpire(c->db,key,time(NULL)+seconds);
20 addReply(c, nx ? shared.cone : shared.ok);
21 }
22
13琛屽鐞?/span>"Set the value of a key, only if the key does not exist"鐨勫満鏅紝17琛屾彃鍏ヨ繖涓?/span>key錛?/span>19琛岃緗畠鐨勮秴鏃訛紝娉ㄦ剰鏃墮棿鎴沖凡緇忚璁劇疆鎴愪簡鍒版湡鏃墮棿銆傝繖閲岃鐪嬩竴涓?/span>redisDb(鍗?/span>c->db)鐨勫畾涔夛細
typedef struct redisDb {
dict *dict; /* The keyspace for this DB */
dict *expires; /* Timeout of keys with a timeout set */
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
dict *io_keys; /* Keys with clients waiting for VM I/O */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
int id;
} redisDb;
浠呭叧娉?/span>dict鍜?/span>expires錛屽垎鍒潵瀛?/span>key-value鍜屽畠鐨勮秴鏃訛紝涔熷氨鏄濡傛灉涓涓?/span>key-value鏄湁瓚呮椂鐨勶紝閭d箞瀹冧細瀛樺湪dict閲岋紝鍚屾椂涔熷瓨鍒?/span>expires閲岋紝綾諱技榪欐牱鐨勫艦寮忥細dict[key]:value,expires[key]:timeout.
褰撶劧key-value娌℃湁瓚呮椂錛?/span>expires閲屽氨涓嶅瓨鍦ㄨ繖涓?/span>key銆?/span>鍓╀笅setKey鍜?/span>setExpire涓や釜鍑芥暟鏃犻潪鏄彃鏁版嵁鍒頒袱涓瓧鍏擱噷錛岃繖閲屼笉鍐嶈榪般?/span>
閭d箞redis鏄浣曞垹闄よ繃鏈?/span>key鐨勫憿銆?/span>
閫氳繃鏌ョ湅dbDelete鐨勮皟鐢ㄨ咃紝棣栧厛娉ㄦ剰鍒拌繖涓涓嚱鏁幫紝鏄敤鏉ュ垹闄よ繃鏈?/span>key鐨勩?/span>
1 int expireIfNeeded(redisDb *db, robj *key) {
2 time_t when = getExpire(db,key);
3
4 if (when < 0) return 0; /* No expire for this key */
5
6 /* Don't expire anything while loading. It will be done later. */
7 if (server.loading) return 0;
8
9 /* If we are running in the context of a slave, return ASAP:
10 * the slave key expiration is controlled by the master that will
11 * send us synthesized DEL operations for expired keys.
12 *
13 * Still we try to return the right information to the caller,
14 * that is, 0 if we think the key should be still valid, 1 if
15 * we think the key is expired at this time. */
16 if (server.masterhost != NULL) {
17 return time(NULL) > when;
18 }
19
20 /* Return when this key has not expired */
21 if (time(NULL) <= when) return 0;
22
23 /* Delete the key */
24 server.stat_expiredkeys++;
25 propagateExpire(db,key);
26 return dbDelete(db,key);
27 }
28
ifNeed琛ㄧず鑳藉垹鍒欏垹錛屾墍浠?/span>4琛屾病鏈夎緗秴鏃朵笉鍒狅紝7琛屽湪"loading"鏃朵笉鍒狅紝16琛岄潪涓誨簱涓嶅垹錛?/span>21琛屾湭鍒版湡涓嶅垹銆?/span>25琛屽悓姝ヤ粠搴撳拰鏂囦歡銆?/span>
鍐嶇湅鐪嬪摢浜涘嚱鏁拌皟鐢ㄤ簡expireIfNeeded錛屾湁lookupKeyRead錛?/span>lookupKeyWrite錛?/span>dbRandomKey錛?/span>existsCommand錛?/span>keysCommand銆傞氳繃榪欎簺鍑芥暟鍛藉悕鍙互鐪嬪嚭錛屽彧瑕佽闂簡鏌愪竴涓?/span>key錛岄『甯﹀仛鐨勪簨鎯呭氨鏄皾璇曟煡鐪嬭繃鏈熷茍鍒犻櫎錛岃繖灝變繚璇佷簡鐢ㄦ埛涓嶅彲鑳借闂埌榪囨湡鐨?/span>key銆備絾鏄鏋滄湁澶ч噺鐨?/span>key榪囨湡錛屽茍涓旀病鏈夎璁塊棶鍒幫紝閭d箞灝辨氮璐逛簡璁稿鍐呭瓨銆?/span>Redis鏄浣曞鐞嗚繖涓棶棰樼殑鍛€?/span>
dbDelete鐨勮皟鐢ㄨ呴噷榪樺彂鐜拌繖鏍蜂竴涓嚱鏁幫細
1 /* Try to expire a few timed out keys. The algorithm used is adaptive and
2 * will use few CPU cycles if there are few expiring keys, otherwise
3 * it will get more aggressive to avoid that too much memory is used by
4 * keys that can be removed from the keyspace. */
5 void activeExpireCycle(void) {
6 int j;
7
8 for (j = 0; j < server.dbnum; j++) {
9 int expired;
10 redisDb *db = server.db+j;
11
12 /* Continue to expire if at the end of the cycle more than 25%
13 * of the keys were expired. */
14 do {
15 long num = dictSize(db->expires);
16 time_t now = time(NULL);
17
18 expired = 0;
19 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
20 num = REDIS_EXPIRELOOKUPS_PER_CRON;
21 while (num--) {
22 dictEntry *de;
23 time_t t;
24
25 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
26 t = (time_t) dictGetEntryVal(de);
27 if (now > t) {
28 sds key = dictGetEntryKey(de);
29 robj *keyobj = createStringObject(key,sdslen(key));
30
31 propagateExpire(db,keyobj);
32 dbDelete(db,keyobj);
33 decrRefCount(keyobj);
34 expired++;
35 server.stat_expiredkeys++;
36 }
37 }
38 } while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4);
39 }
40 }
41
榪欎釜鍑芥暟鐨勬剰鍥懼凡緇忔湁璇存槑錛?/span>鍒犱竴鐐圭偣榪囨湡key錛屽鏋滆繃鏈?/span>key杈冨皯錛岄偅涔熷彧鐢ㄤ竴鐐圭偣cpu銆?/span>25琛岄殢鏈哄彇涓涓?/span>key錛?/span>38琛屽垹key鎴愬姛鐨勬鐜囪緝浣庡氨閫鍑恒傝繖涓嚱鏁拌鏀懼湪涓涓?/span>cron閲岋紝姣忔縐掕璋冪敤涓嬈°傝繖涓畻娉曚繚璇佹瘡嬈′細鍒犻櫎涓瀹氭瘮渚嬬殑key錛屼絾鏄鏋?/span>key鎬婚噺寰堝ぇ錛岃岃繖涓瘮渚嬫帶鍒剁殑澶ぇ錛屽氨闇瑕佹洿澶氭鐨勫驚鐜紝嫻垂cpu錛屾帶鍒剁殑澶皬錛岃繃鏈熺殑key灝變細鍙樺錛屾氮璐瑰唴瀛?#8212;—榪欏氨鏄椂絀烘潈琛′簡銆?/span>
鏈鍚庡湪dbDelete鐨勮皟鐢ㄨ呴噷榪樺彂鐜拌繖鏍蜂竴涓嚱鏁幫細
/* This function gets called when 'maxmemory' is set on the config file to limit
* the max memory used by the server, and we are out of memory.
* This function will try to, in order:
*
* - Free objects from the free list
* - Try to remove keys with an EXPIRE set
*
* It is not possible to free enough memory to reach used-memory < maxmemory
* the server will start refusing commands that will enlarge even more the
* memory usage.
*/
void freeMemoryIfNeeded(void)
榪欎釜鍑芥暟澶暱灝變笉鍐嶈榪頒簡錛屾敞閲婇儴鍒嗚鏄庡彧鏈夊湪閰嶇疆鏂囦歡涓緗簡鏈澶у唴瀛樻椂鍊欐墠浼氳皟鐢ㄨ繖涓嚱鏁幫紝鑰岃緗繖涓弬鏁扮殑鎰忎箟鏄紝浣犳妸redis褰撳仛涓涓唴瀛?/span>cache鑰屼笉鏄?/span>key-value鏁版嵁搴撱?/span>
浠ヤ笂3縐嶅垹闄よ繃鏈?/span>key鐨勯斿緞錛岀浜岀瀹氭湡鍒犻櫎涓瀹氭瘮渚嬬殑key鏄富瑕佺殑鍒犻櫎閫斿緞錛岀涓縐?#8220;璇繪椂鍒犻櫎”淇濊瘉榪囨湡key涓嶄細琚闂埌錛岀涓夌鏄竴涓綋鍐呭瓨瓚呭嚭璁懼畾鏃剁殑鏆村姏鎵嬫銆傜敱姝や篃鑳界湅鍑?/span>redis璁捐鐨勫閥濡欎箣澶勶紝

]]>