當(dāng)redis使用的內(nèi)存超過了設(shè)置的最大內(nèi)存時(shí),,會(huì)觸發(fā)redis的key淘汰機(jī)制,,在redis 3.0中有6種淘汰策略: noeviction: 不刪除策略。當(dāng)達(dá)到最大內(nèi)存限制時(shí), 如果需要使用更多內(nèi)存,則直接返回錯(cuò)誤信息,。(redis默認(rèn)淘汰策略) allkeys-lru: 在所有key中優(yōu)先刪除最近最少使用(less recently used ,LRU) 的 key,。 allkeys-random: 在所有key中隨機(jī)刪除一部分 key。 volatile-lru: 在設(shè)置了超時(shí)時(shí)間(expire )的key中優(yōu)先刪除最近最少使用(less recently used ,LRU) 的 key,。 volatile-random: 在設(shè)置了超時(shí)時(shí)間(expire)的key中隨機(jī)刪除一部分 key,。 volatile-ttl: 在設(shè)置了超時(shí)時(shí)間(expire )的key中優(yōu)先刪除剩余時(shí)間(time to live,TTL) 短的key。 場(chǎng)景: 數(shù)據(jù)庫(kù)中有1000w的數(shù)據(jù),,而redis中只有50w數(shù)據(jù),,如何保證redis中10w數(shù)據(jù)都是熱點(diǎn)數(shù)據(jù)? 方案: 限定 Redis 占用的內(nèi)存,,Redis 會(huì)根據(jù)自身數(shù)據(jù)淘汰策略,,留下熱數(shù)據(jù)到內(nèi)存。所以,,計(jì)算一下 50W 數(shù)據(jù)大約占用的內(nèi)存,,然后設(shè)置一下 Redis 內(nèi)存限制即可,并將淘汰策略為volatile-lru或者allkeys-lru,。 設(shè)置Redis最大占用內(nèi)存: 打開redis配置文件,,設(shè)置maxmemory參數(shù),maxmemory是bytes字節(jié)類型 # In short... if you have slaves attached it is suggested that you set a lower # limit for maxmemory so that there is some free RAM on the system for slave # output buffers (but this is not needed if the policy is 'noeviction'). # # maxmemory <bytes> maxmemory 268435456 設(shè)置過期策略: maxmemory-policy volatile-lru ———————————————— 版權(quán)聲明:本文為CSDN博主「代碼搬運(yùn)工.」的原創(chuàng)文章,,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/u013308490/article/details/87737810 |
|