用于内存刷新的Redis脚本

用于内存刷新的Redis脚本,redis,Redis,是否可以为Redis创建一个脚本,当它高于某个值时刷新它的内存? 在我的特定情况下,当内存超过90%时,我需要刷新。 通过bash脚本或Lua脚本,最好的方法是什么?这是获取已分配内存redis cli-h 1.2.3.4-p 6379内存统计数据的方法。现在创建bash脚本很容易了,我将使用Lua脚本,因为它执行速度更快、原子化,而且从rediscli和任何应用程序代码都很容易使用 这里有一个Lua脚本,用于获取已使用的内存和maxmemory、百分比以及动作占位符。它使用内存统计和信息内存来

是否可以为Redis创建一个脚本,当它高于某个值时刷新它的内存? 在我的特定情况下,当内存超过90%时,我需要刷新。
通过bash脚本或Lua脚本,最好的方法是什么?

这是获取已分配内存redis cli-h 1.2.3.4-p 6379内存统计数据的方法。现在创建bash脚本很容易了,我将使用Lua脚本,因为它执行速度更快、原子化,而且从rediscli和任何应用程序代码都很容易使用

这里有一个Lua脚本,用于获取已使用的内存和maxmemory、百分比以及动作占位符。它使用内存统计和信息内存来说明

MEMORY STATS提供结构化信息,但不包括maxmemory或total_system_MEMORY,如INFO MEMORY。不允许从Lua脚本获取配置

local stats = redis.call('MEMORY', 'STATS')
local memused = 0
for i = 1,table.getn(stats),2 do
    if stats[i] == 'total.allocated' then
        memused = stats[i+1]
        break
    end
end
local meminfo = redis.call('INFO', 'memory')
local maxmemory = 0
for s in meminfo:gmatch('[^\\r\\n]+') do
    if string.sub(s,1,10) == 'maxmemory:' then
        maxmemory = tonumber(string.sub(s,11))
    end
end
local mempercent = memused/maxmemory
local action = 'No action'
if mempercent > tonumber(ARGV[1]) then
    action = 'Flush here'
end
return {memused, maxmemory, tostring(mempercent), action}
用作:

> EVAL "local stats = redis.call('MEMORY', 'STATS') \n local memused = 0 \n for i = 1,table.getn(stats),2 do \n     if stats[i] == 'total.allocated' then \n     memused = stats[i+1] \n break \n end \n end \n local meminfo = redis.call('INFO', 'memory') \n local maxmemory = 0 \n for s in meminfo:gmatch('[^\\r\\n]+') do \n     if string.sub(s,1,10) == 'maxmemory:' then \n     maxmemory = tonumber(string.sub(s,11)) \n end \n end \n local mempercent = memused/maxmemory \n local action = 'No action' \n if mempercent > tonumber(ARGV[1]) then \n     action = 'Flush here' \n end \n return {memused, maxmemory, tostring(mempercent), action}" 0 0.9
1) (integer) 860264
2) (integer) 100000000
3) "0.00860264"
4) "No action"

这回答了你的问题吗?这不能回答我。可能要了解更多信息,我正在使用GCP MemoryStore如果我使用printf MEMORY STATS\r\n;|nc REDIS_IP 6379 I获取所有值。有没有办法只看到一片田地?我正在寻找一个类似grepMaybe的命令,这个命令比第一个命令更有用