File: neural_maybe_invalidate.lua

package info (click to toggle)
rspamd 3.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 35,064 kB
  • sloc: ansic: 247,728; cpp: 107,741; javascript: 31,385; perl: 3,089; asm: 2,512; pascal: 1,625; python: 1,510; sh: 589; sql: 313; makefile: 195; xml: 74
file content (25 lines) | stat: -rw-r--r-- 754 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- Lua script to invalidate ANNs by rank
-- Uses the following keys
-- key1 - prefix for keys
-- key2 - number of elements to leave

local card = redis.call('ZCARD', KEYS[1])
local lim = tonumber(KEYS[2])
if card > lim then
  local to_delete = redis.call('ZRANGE', KEYS[1], 0, card - lim - 1)
  if to_delete then
    for _, k in ipairs(to_delete) do
      local tb = cjson.decode(k)
      if type(tb) == 'table' and type(tb.redis_key) == 'string' then
        redis.call('DEL', tb.redis_key)
        -- Also train vectors
        redis.call('DEL', tb.redis_key .. '_spam_set')
        redis.call('DEL', tb.redis_key .. '_ham_set')
      end
    end
  end
  redis.call('ZREMRANGEBYRANK', KEYS[1], 0, card - lim - 1)
  return to_delete
else
  return {}
end