当前位置:网站首页>The life cycle of key value in redis module programming
The life cycle of key value in redis module programming
2022-04-23 15:04:00 【Mrpre】
RedisModule_OpenKey
This section discusses RedisModule_OpenKey in key-value When to release .
First The data structure is as follows
struct RedisModuleKey {
....
robj *key;
robj *value;
....
}
What we care about is RedisModuleKey And among Of key and value The life cycle of the value
RedisModuleKey In itself adopt RedisModule_OpenKey apply , Module memory management will automatically RedisModuleKey Save in ctx in . After the instruction execution is completed , call moduleFreeContext->autoMemoryCollect->RM_CloseKey->moduleCloseKey Try to release .
You can see A complete command ,RedisModuleKey Itself will be ruthlessly released .
void RM_CloseKey(RedisModuleKey *key) {
if (key == NULL) return;
moduleCloseKey(key);
autoMemoryFreed(key->ctx,REDISMODULE_AM_KEY,key);
zfree(key);// Completely release
}
therefore ,RedisModule_OpenKey The essence of can be understood as just a Temporary variable ,
Now let's look at RedisModuleKey Among them key value , As you can see below ,key The reference count of minus 1.
/* Destroy a RedisModuleKey struct (freeing is the responsibility of the caller). */
static void moduleCloseKey(RedisModuleKey *key) {
int signal = SHOULD_SIGNAL_MODIFIED_KEYS(key->ctx);
if ((key->mode & REDISMODULE_WRITE) && signal)
signalModifiedKey(key->ctx->client,key->db,key->key);
/* TODO: if (key->iter) RM_KeyIteratorStop(kp); */
RM_ZsetRangeStop(key);
decrRefCount(key->key);
}
therefore key Will be ruthlessly released ? not always , If RedisModuleKey Participating key The reference count is 1 And the mode is write Pattern , So in RM_OpenKey->moduleInitKey in , The reference count will be ++. That is to say RedisModule_OpenKey After the return ,key Add... To the original reference count 1, So in moduleCloseKey in ,key Will not be released .
If If you use this :RedisModule_OpenKey(ctx, argv[1]..., That is, I like to take the user's input as open Parameters of , although RedisModule_OpenKey After the return argv[1] The reference count for is 2, however arg The parameter itself will be in redis The outermost layer of the frame is released once , therefore argv[1] Will not be kept in memory .
however If you use this :RedisModule_OpenKey(ctx, RedisModule_CreateString(NULL, s, (size_t)slen)...), that No one will release RedisModule_CreateString Created memory , At this point either RedisModule_CreateString The first input parameter is passed into ctx Then rely on redis Automatic memory management to automatically free memory , Or take the initiative to call free To reduce the reference count .
Look again. value value ,module adopt RedisModule_ModuleTypeSetValue To set up value value , Its essence is genericSetKey, take RedisModuleKey Medium key-value Save to redis Of db in ,
int RM_ModuleTypeSetValue(RedisModuleKey *key, moduleType *mt, void *value) {
if (!(key->mode & REDISMODULE_WRITE) || key->iter) return REDISMODULE_ERR;
/* old value If there is , For example, you have executed this function before , old value The reference count is decremented 1*/
RM_DeleteKey(key);
robj *o = createModuleObject(mt,value);
genericSetKey(key->ctx->client,key->db,key->key,o,0,0);
/* Here we are o The app count for is 2*/
decrRefCount(o);
/* Here we are o The app count for is 1*/
key->value = o;
return REDISMODULE_OK;
}
therefore ,value Reference count of value , stay adopt RedisModule_ModuleTypeSetValue There is no increase or decrease after . That is to say value The life cycle of the value does not change .
RedisModule_DeleteKey In fact, that is dbDelete Encapsulation ,RedisModule_ModuleTypeSetValue take key-value Save in db in , Then they didn't increase or decrease their reference count ,RedisModule_DeleteKey Because of the call dbDelete So it's going to go straight to theta key as well as value The reference count of minus 1.
Sum up :
1、RedisModule_OpenKey Back to RedisModuleKey The pointer , Will be released anyway . among ,RedisModule_OpenKey It will be involved in key, Reference count plus one . Then release RedisModuleKey when , reduce 1, If you need this elsewhere If Enter the reference key The original reference count was 1, Then additional operations are required to release it .
2、 If it does RedisModule_ModuleTypeSetValue, that ,RedisModuleKey The total key and value Be existed db, among ,key It's a copy , All of us don't care .value By db Direct reference ( The pointer points directly to value).RedisModule_ModuleTypeSetValue Not by itself key and value Add or delete reference count .
3、 adopt DEL command , Delete RedisModule_OpenKey Specified in the key when , Same as standard process , Yes value Quote count minus 1,RedisModule_DeleteKey The same logic .
版权声明
本文为[Mrpre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231409587951.html
边栏推荐
- like和regexp差别
- JS -- realize click Copy function
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
- Realization of four data flow modes of grpc based on Multilingual Communication
- I/O复用的高级应用:同时处理 TCP 和 UDP 服务
- How to upload large files quickly?
- Tencent has written a few words, Ali has written them all for a month
- Leetcode165 compare version number double pointer string
- Little red book timestamp2 (2022 / 04 / 22)
- Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
猜你喜欢

Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge

Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring

Nuxt project: Global get process Env information

Brute force of DVWA low -- > High

Role of asemi rectifier module mdq100-16 in intelligent switching power supply

编程哲学——自动加载、依赖注入与控制反转

The win10 taskbar notification area icon is missing

Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success

UML learning_ Day2

免费在upic中设置OneDrive或Google Drive作为图床
随机推荐
Share 20 tips for ES6 that should not be missed
Resolve the conflict between computed attribute and input blur event
Little red book timestamp2 (2022 / 04 / 22)
What is the main purpose of PCIe X1 slot?
Async void caused the program to crash
Epolloneshot event of epoll -- instance program
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
For 22 years, you didn't know the file contained vulnerabilities?
三、梯度下降求解最小θ
win10 任务栏通知区图标不见了
Five data types of redis
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
Brute force of DVWA low -- > High
冰冰学习笔记:一步一步带你实现顺序表
LeetCode149-直线上最多的点数-数学-哈希表
脏读、不可重复读和幻读介绍
1-初识Go语言
分享3个使用工具,在家剪辑5个作品挣了400多
Llvm - generate addition
Leetcode153 - find the minimum value in the rotation sort array - array - binary search