锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲αv久久久噜噜噜噜噜,久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 ,亚洲AV无码久久精品狠狠爱浪潮http://www.shnenglu.com/richbirdandy/category/18254.htmlzh-cnWed, 30 Nov 2011 14:11:43 GMTWed, 30 Nov 2011 14:11:43 GMT60redis濡備綍鍒犻櫎榪囨湡鏁版嵁http://www.shnenglu.com/richbirdandy/archive/2011/11/29/161184.html闃夸簩闃夸簩Tue, 29 Nov 2011 11:57:00 GMThttp://www.shnenglu.com/richbirdandy/archive/2011/11/29/161184.htmlhttp://www.shnenglu.com/richbirdandy/comments/161184.htmlhttp://www.shnenglu.com/richbirdandy/archive/2011/11/29/161184.html#Feedback1http://www.shnenglu.com/richbirdandy/comments/commentRss/161184.htmlhttp://www.shnenglu.com/richbirdandy/services/trackbacks/161184.html
闅忕潃nosql椋庢疆鍏磋搗錛?/span>redis浣滀負(fù)褰撲腑涓涓鐪肩殑鏄庢槦錛屼篃瓚婃潵瓚婂鐨勮鍏蟲(chóng)敞鍜屼嬌鐢紝鎴戝湪宸ヤ綔涓篃騫挎硾鐨勭敤鍒頒簡(jiǎn)redis鏉ュ厖褰?/span>cache鍜?/span>key-value DB錛屼絾褰撳ぇ瀹跺彂鐜版暟鎹秺鏉ヨ秺澶氭椂錛屼笉紱佹湁浜涙媴蹇?jī)锛?/span>redis鑳芥拺鐨勪綇鍚楋紝铏界劧瀹樻柟宸茬粡鏈夋紓浜殑benchmark錛岃嚜宸變篃鍙互鍋氬仛鍘嬪姏嫻嬭瘯錛屼絾鏄湅鐪嬫簮鐮侊紝涔熸槸紜闂鏈鐩存帴鐨勫姙娉曚箣涓銆傛瘮濡傜洰鍓嶆垜浠紜鐨勪竴涓棶棰樻槸錛?/span>redis鏄浣曞垹闄よ繃鏈熸暟鎹殑錛?/span>

鐢ㄤ竴涓彲浠?/span>"find reference"鐨?/span>IDE,娌跨潃setex(Set the value and expiration of a key)鍛戒護(hù)涓紿ョ┒绔燂細(xì)

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琛岃緗畠鐨勮秴鏃訛紝娉ㄦ剰鏃墮棿鎴沖凡緇忚璁劇疆鎴愪簡(jiǎn)鍒版湡鏃墮棿銆傝繖閲岃鐪嬩竴涓?/span>redisDb(鍗?/span>c->db)鐨勫畾涔夛細(xì)

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箞瀹冧細(xì)瀛樺湪dict閲岋紝鍚屾椂涔熷瓨鍒?/span>expires閲岋紝綾諱技榪欐牱鐨勫艦寮忥細(xì)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>

鍐嶇湅鐪嬪摢浜涘嚱鏁拌皟鐢ㄤ簡(jiǎn)expireIfNeeded錛屾湁lookupKeyRead錛?/span>lookupKeyWrite錛?/span>dbRandomKey錛?/span>existsCommand錛?/span>keysCommand銆傞氳繃榪欎簺鍑芥暟鍛藉悕鍙互鐪嬪嚭錛屽彧瑕佽闂簡(jiǎn)鏌愪竴涓?/span>key錛岄『甯﹀仛鐨勪簨鎯呭氨鏄皾璇曟煡鐪嬭繃鏈熷茍鍒犻櫎錛岃繖灝變繚璇佷簡(jiǎn)鐢ㄦ埛涓嶅彲鑳借闂埌榪囨湡鐨?/span>key銆備絾鏄鏋滄湁澶ч噺鐨?/span>key榪囨湡錛屽茍涓旀病鏈夎璁塊棶鍒幫紝閭d箞灝辨氮璐逛簡(jiǎn)璁稿鍐呭瓨銆?/span>Redis鏄浣曞鐞嗚繖涓棶棰樼殑鍛€?/span>


dbDelete鐨勮皟鐢ㄨ呴噷榪樺彂鐜拌繖鏍蜂竴涓嚱鏁幫細(xì)

 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閲岋紝姣忔縐掕璋冪敤涓嬈°傝繖涓畻娉曚繚璇佹瘡嬈′細(xì)鍒犻櫎涓瀹氭瘮渚嬬殑key錛屼絾鏄鏋?/span>key鎬婚噺寰堝ぇ錛岃岃繖涓瘮渚嬫帶鍒剁殑澶ぇ錛屽氨闇瑕佹洿澶氭鐨勫驚鐜紝嫻垂cpu錛屾帶鍒剁殑澶皬錛岃繃鏈熺殑key灝變細(xì)鍙樺錛屾氮璐瑰唴瀛?#8212;—榪欏氨鏄椂絀烘潈琛′簡(jiǎn)銆?/span>

 

鏈鍚庡湪dbDelete鐨勮皟鐢ㄨ呴噷榪樺彂鐜拌繖鏍蜂竴涓嚱鏁幫細(xì)

/* 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)

榪欎釜鍑芥暟澶暱灝變笉鍐嶈榪頒簡(jiǎn)錛屾敞閲婇儴鍒嗚鏄庡彧鏈夊湪閰嶇疆鏂囦歡涓緗簡(jiǎn)鏈澶у唴瀛樻椂鍊欐墠浼?xì)璋冪敤杩欎釜鍑芥晭图岃岃緗繖涓弬鏁扮殑鎰忎箟鏄紝浣犳妸redis褰撳仛涓涓唴瀛?/span>cache鑰屼笉鏄?/span>key-value鏁版嵁搴撱?/span>


浠ヤ笂3縐嶅垹闄よ繃鏈?/span>key鐨勯斿緞錛岀浜岀瀹氭湡鍒犻櫎涓瀹氭瘮渚嬬殑key鏄富瑕佺殑鍒犻櫎閫斿緞錛岀涓縐?#8220;璇繪椂鍒犻櫎”淇濊瘉榪囨湡key涓嶄細(xì)琚闂埌錛岀涓夌鏄竴涓綋鍐呭瓨瓚呭嚭璁懼畾鏃剁殑鏆村姏鎵嬫銆傜敱姝や篃鑳界湅鍑?/span>redis璁捐鐨勫閥濡欎箣澶勶紝



闃夸簩 2011-11-29 19:57 鍙戣〃璇勮
]]>
中文成人无码精品久久久不卡| 国产精品久久久久久久人人看| 国产91色综合久久免费| 国内精品人妻无码久久久影院| 久久国产高潮流白浆免费观看| 久久不射电影网| 欧美无乱码久久久免费午夜一区二区三区中文字幕| 国内精品欧美久久精品| 精品国产乱码久久久久久人妻| 免费国产99久久久香蕉| 伊人久久大香线蕉亚洲五月天| 国产69精品久久久久99| 午夜久久久久久禁播电影| 久久国产三级无码一区二区| 香蕉久久夜色精品升级完成| 青青青青久久精品国产h久久精品五福影院1421 | 青青青国产精品国产精品久久久久| 色欲综合久久躁天天躁| 91精品国产综合久久四虎久久无码一级| 久久婷婷人人澡人人爽人人爱| 久久精品亚洲男人的天堂| 精品久久无码中文字幕| 99久久精品免费看国产一区二区三区| 国产精品一区二区久久精品无码| 99久久99久久久精品齐齐| 久久人人爽爽爽人久久久| 久久亚洲sm情趣捆绑调教| 久久婷婷人人澡人人| 午夜不卡888久久| 国产精品久久久久久福利69堂| 久久综合久久自在自线精品自| 久久国语露脸国产精品电影| 伊人色综合久久天天人守人婷| 伊人热热久久原色播放www| 亚洲国产精品嫩草影院久久| 久久夜色撩人精品国产| 日批日出水久久亚洲精品tv| 久久无码一区二区三区少妇| 久久亚洲电影| 亚洲va久久久噜噜噜久久| 久久丫精品国产亚洲av|