summaryrefslogtreecommitdiff
path: root/src/object.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/object.c')
-rw-r--r--src/object.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/object.c b/src/object.c
index a77f2aeda..89166582b 100644
--- a/src/object.c
+++ b/src/object.c
@@ -1166,6 +1166,32 @@ sds getMemoryDoctorReport(void) {
return s;
}
+/* Set the object LRU/LFU depending on server.maxmemory_policy.
+ * The lfu_freq arg is only relevant if policy is MAXMEMORY_FLAG_LFU.
+ * The lru_idle and lru_clock args are only relevant if policy
+ * is MAXMEMORY_FLAG_LRU.
+ * Either or both of them may be <0, in that case, nothing is set. */
+void objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
+ long long lru_clock) {
+ if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
+ if (lfu_freq >= 0) {
+ serverAssert(lfu_freq <= 255);
+ val->lru = (LFUGetTimeInMinutes()<<8) | lfu_freq;
+ }
+ } else if (lru_idle >= 0) {
+ /* Serialized LRU idle time is in seconds. Scale
+ * according to the LRU clock resolution this Redis
+ * instance was compiled with (normally 1000 ms, so the
+ * below statement will expand to lru_idle*1000/1000. */
+ lru_idle = lru_idle*1000/LRU_CLOCK_RESOLUTION;
+ val->lru = lru_clock - lru_idle;
+ /* If the lru field overflows (since LRU it is a wrapping
+ * clock), the best we can do is to provide the maximum
+ * representable idle time. */
+ if (val->lru < 0) val->lru = lru_clock+1;
+ }
+}
+
/* ======================= The OBJECT and MEMORY commands =================== */
/* This is a helper function for the OBJECT command. We need to lookup keys