锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产69精品久久久久APP下载,一本色道久久88综合日韩精品 ,国产69精品久久久久99尤物http://m.shnenglu.com/richbirdandy/category/18254.htmlzh-cnWed, 30 Nov 2011 14:11:43 GMTWed, 30 Nov 2011 14:11:43 GMT60redis濡備綍鍒犻櫎榪囨湡鏁版嵁http://m.shnenglu.com/richbirdandy/archive/2011/11/29/161184.html闃夸簩闃夸簩Tue, 29 Nov 2011 11:57:00 GMThttp://m.shnenglu.com/richbirdandy/archive/2011/11/29/161184.htmlhttp://m.shnenglu.com/richbirdandy/comments/161184.htmlhttp://m.shnenglu.com/richbirdandy/archive/2011/11/29/161184.html#Feedback1http://m.shnenglu.com/richbirdandy/comments/commentRss/161184.htmlhttp://m.shnenglu.com/richbirdandy/services/trackbacks/161184.html
闅忕潃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 < 0return 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璁捐鐨勫閥濡欎箣澶勶紝



闃夸簩 2011-11-29 19:57 鍙戣〃璇勮
]]>
久久久青草青青国产亚洲免观| 狠狠色丁香婷婷综合久久来| 久久精品男人影院| 久久综合狠狠色综合伊人| 精品久久久久久亚洲| 色偷偷88欧美精品久久久| 久久天天躁狠狠躁夜夜avapp| 久久99精品国产一区二区三区| 四虎久久影院| aaa级精品久久久国产片| 久久一区二区三区免费| 久久国产欧美日韩精品| 亚洲欧洲中文日韩久久AV乱码| 99久久国语露脸精品国产| 亚洲欧美一级久久精品| 精品久久久久久久久中文字幕| 久久中文字幕视频、最近更新| 成人久久综合网| 国内精品综合久久久40p| 色综合久久中文字幕综合网| 亚洲一区中文字幕久久| 久久久久亚洲AV无码麻豆| 精品久久久久久无码不卡| 久久涩综合| 久久99精品国产麻豆蜜芽| 狠狠久久亚洲欧美专区| 亚洲AV无码久久精品蜜桃| 亚洲精品成人网久久久久久| 久久综合伊人77777麻豆| 国内精品久久久久久久久| 91麻精品国产91久久久久| 久久免费视频观看| 99久久亚洲综合精品成人| yellow中文字幕久久网| 99久久亚洲综合精品成人| 日韩精品久久久久久| 久久美女网站免费| 国产福利电影一区二区三区,免费久久久久久久精 | 久久精品国产福利国产秒| 亚洲av日韩精品久久久久久a| 中文字幕乱码久久午夜|