added noeviction policy to redis maxmemory. ZSCORE removed from the list of commands that can't be called when we are low on memory, this command was added in the past for a stupid error.

This commit is contained in:
antirez 2010-11-08 16:12:16 +01:00
parent 240f8dbf3f
commit 5402c4262e
4 changed files with 21 additions and 1 deletions

View File

@ -191,6 +191,18 @@ slave-serve-stale-data yes
# volatile-random -> remove a random key with an expire set # volatile-random -> remove a random key with an expire set
# allkeys->random -> remove a random key, any key # allkeys->random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL) # volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
#
# Note: with all the kind of policies, Redis will return an error on write
# operations, when there are not suitable keys for eviction.
#
# At the date of writing this commands are: set setnx setex append
# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
# getset mset msetnx exec sort
#
# The default is:
# #
# maxmemory-policy volatile-lru # maxmemory-policy volatile-lru

View File

@ -136,6 +136,8 @@ void loadServerConfig(char *filename) {
server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU; server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
} else if (!strcasecmp(argv[1],"allkeys-random")) { } else if (!strcasecmp(argv[1],"allkeys-random")) {
server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM; server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
} else if (!strcasecmp(argv[1],"noeviction")) {
server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
} else { } else {
err = "Invalid maxmemory policy"; err = "Invalid maxmemory policy";
goto loaderr; goto loaderr;
@ -307,6 +309,8 @@ void configSetCommand(redisClient *c) {
server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU; server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
} else if (!strcasecmp(o->ptr,"allkeys-random")) { } else if (!strcasecmp(o->ptr,"allkeys-random")) {
server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM; server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
} else if (!strcasecmp(o->ptr,"noeviction")) {
server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
} else { } else {
goto badfmt; goto badfmt;
} }
@ -440,6 +444,7 @@ void configGetCommand(redisClient *c) {
case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break; case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break;
case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break; case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break;
case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break; case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break;
case REDIS_MAXMEMORY_NO_EVICTION: s = "noeviction"; break;
default: s = "unknown"; break; /* too harmless to panic */ default: s = "unknown"; break; /* too harmless to panic */
} }
addReplyBulkCString(c,"maxmemory-policy"); addReplyBulkCString(c,"maxmemory-policy");

View File

@ -124,7 +124,7 @@ struct redisCommand readonlyCommandTable[] = {
{"zcount",zcountCommand,4,0,NULL,1,1,1}, {"zcount",zcountCommand,4,0,NULL,1,1,1},
{"zrevrange",zrevrangeCommand,-4,0,NULL,1,1,1}, {"zrevrange",zrevrangeCommand,-4,0,NULL,1,1,1},
{"zcard",zcardCommand,2,0,NULL,1,1,1}, {"zcard",zcardCommand,2,0,NULL,1,1,1},
{"zscore",zscoreCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1}, {"zscore",zscoreCommand,3,0,NULL,1,1,1},
{"zrank",zrankCommand,3,0,NULL,1,1,1}, {"zrank",zrankCommand,3,0,NULL,1,1,1},
{"zrevrank",zrevrankCommand,3,0,NULL,1,1,1}, {"zrevrank",zrevrankCommand,3,0,NULL,1,1,1},
{"hset",hsetCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1}, {"hset",hsetCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1},
@ -1332,6 +1332,8 @@ void monitorCommand(redisClient *c) {
void freeMemoryIfNeeded(void) { void freeMemoryIfNeeded(void) {
/* Remove keys accordingly to the active policy as long as we are /* Remove keys accordingly to the active policy as long as we are
* over the memory limit. */ * over the memory limit. */
if (server.maxmemory_policy == REDIS_MAXMEMORY_NO_EVICTION) return;
while (server.maxmemory && zmalloc_used_memory() > server.maxmemory) { while (server.maxmemory && zmalloc_used_memory() > server.maxmemory) {
int j, k, freed = 0; int j, k, freed = 0;

View File

@ -209,6 +209,7 @@
#define REDIS_MAXMEMORY_VOLATILE_RANDOM 2 #define REDIS_MAXMEMORY_VOLATILE_RANDOM 2
#define REDIS_MAXMEMORY_ALLKEYS_LRU 3 #define REDIS_MAXMEMORY_ALLKEYS_LRU 3
#define REDIS_MAXMEMORY_ALLKEYS_RANDOM 4 #define REDIS_MAXMEMORY_ALLKEYS_RANDOM 4
#define REDIS_MAXMEMORY_NO_EVICTION 5
/* We can print the stacktrace, so our assert is defined this way: */ /* We can print the stacktrace, so our assert is defined this way: */
#define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1))) #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))