A memory management:
command |
effect |
expire key seconds |
Set a timeout timestamp in seconds |
ttl key |
See timeout, -1 means no timeout, -2 represent has expired |
persist key |
Persistence Key , cancel timeout |
1, redis-cli:
2, spring operation:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:redis.xml") public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void testExpireTime() { redisTemplate.execute(new SessionCallback() { public Object execute(RedisOperations operations) throws DataAccessException { ValueOperations ops = operations.opsForValue(); ops.set("myhome", "China"); operations.expire("myhome", 100, TimeUnit.SECONDS); Long time = operations.getExpire("myhome"); System.out.println(time); operations.persist("myhome"); Long time1 = operations.getExpire("myhome"); System.out.println(time1); return null; } }); } }
Second, memory recovery strategy:
1, automatic recovery after a timeout is not, only the mark key has expired;
2, inert recycling: get command has timed recovery keys;
3, redis.conf: configuration hz 1 ~ 100;
To recover // hz frequency: recommended configuration 10;
4, accurate recovery: maxmemory-samples 1 ~ 10
// 10: The most delicate, the slowest; 1: the least accurate and fastest;
5, maxmemory: maximum memory configuration, the memory key exceeds this maximum, the LRU algorithm directly recycled;
5, recovery algorithm:
maxmemory-policy: noeviction // default
Volatile-lru |
Phase out the use of least recently used strategy, Redis will recover those time-out value pairs |
Allkeys-lru |
Redis will have all the key-value pairs, using the least recently used strategy be eliminated |
Volatile-random |
A randomized phase-out policy removes the timeout value pairs |
Allkeys-random |
A randomized phase-out strategy to remove all key-value pairs |
Volatile-ttl |
Delete the shortest survival time using key-value pairs strategy |
Noeviction |
Not out of any key-value pairs, when the memory is full, only supports reading and writing are not supported |
// redis default algorithm is none, but will get the use of inert recycling;
// recommended Volatile-lru and Allkeys-lru algorithm;