Debian Codegraph @

lualib/redis_scripts/bayes_cache_learn.lua

raw
1 -- Lua script to perform cache learning for bayes classification (multi-class)
2 -- This script accepts the following parameters:
3 -- key1 - cache id
4 -- argv1 - class name string (e.g. "spam", "ham", "transactional")
5 -- argv2 - configuration table in message pack
6 --
7 -- The cache value stored in Redis is the class name string. A numeric class_id
8 -- hash was used previously, but uint64_t values > 2^53 lose precision when
9 -- round-tripped through Lua doubles, so the equality check on retrieval was
10 -- unreliable for arbitrary multiclass names.
11
12 local cache_id = KEYS[1]
13 local class_name = ARGV[1]
14 local conf = cmsgpack.unpack(ARGV[2])
15
16 -- Store the class name directly as the cache value
17 local cache_value = class_name
18 cache_id = string.sub(cache_id, 1, conf.cache_elt_len)
19
20 -- Try each prefix that is in Redis (as some other instance might have set it)
21 for i = 0, conf.cache_max_keys do
22 local prefix = conf.cache_prefix .. string.rep("X", i)
23 local have = redis.call('HGET', prefix, cache_id)
24
25 if have then
26 -- Already in cache, but cache_value changes when relearning
27 redis.call('HSET', prefix, cache_id, cache_value)
28 return false
29 end
30 end
31
32 local added = false
33 local lim = conf.cache_max_elt
34 for i = 0, conf.cache_max_keys do
35 if not added then
36 local prefix = conf.cache_prefix .. string.rep("X", i)
37 local count = redis.call('HLEN', prefix)
38
39 if count < lim then
40 -- We can add it to this prefix
41 redis.call('HSET', prefix, cache_id, cache_value)
42 added = true
43 end
44 end
45 end
46
47 if not added then
48 -- Need to expire some keys
49 local expired = false
50 for i = 0, conf.cache_max_keys do
51 local prefix = conf.cache_prefix .. string.rep("X", i)
52 local exists = redis.call('EXISTS', prefix)
53
54 if exists then
55 if not expired then
56 redis.call('DEL', prefix)
57 redis.call('HSET', prefix, cache_id, cache_value)
58
59 -- Do not expire anything else
60 expired = true
61 elseif i > 0 then
62 -- Move key to a shorter prefix, so we will rotate them eventually from lower to upper
63 local new_prefix = conf.cache_prefix .. string.rep("X", i - 1)
64 redis.call('RENAME', prefix, new_prefix)
65 end
66 end
67 end
68 end
69
70 return true