diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-01-12 15:41:11 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-01-12 15:41:11 +0000 |
commit | 7541d281d3376afd1216426d843981b4014569cc (patch) | |
tree | 2c3079b4fdc868d6a354ecfc0f0426b2c9be8c3b /lualib/lua_bayes_redis.lua | |
parent | 48bfe901865d2cab00c2d92372eb16476bd08262 (diff) | |
download | rspamd-7541d281d3376afd1216426d843981b4014569cc.tar.gz rspamd-7541d281d3376afd1216426d843981b4014569cc.zip |
[Project] Initial implementation of the lua counterpart
Diffstat (limited to 'lualib/lua_bayes_redis.lua')
-rw-r--r-- | lualib/lua_bayes_redis.lua | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/lualib/lua_bayes_redis.lua b/lualib/lua_bayes_redis.lua index 6f6da339e..3321c96c3 100644 --- a/lualib/lua_bayes_redis.lua +++ b/lualib/lua_bayes_redis.lua @@ -54,12 +54,12 @@ local function gen_learn_functor(redis_params, learn_script_id) if maybe_text_tokens then lua_redis.exec_redis_script(learn_script_id, - { task = task, is_write = false, key = expanded_key }, + { task = task, is_write = true, key = expanded_key }, learn_redis_cb, { expanded_key, tostring(is_spam), symbol, tostring(is_unlearn), stat_tokens, maybe_text_tokens }) else lua_redis.exec_redis_script(learn_script_id, - { task = task, is_write = false, key = expanded_key }, + { task = task, is_write = true, key = expanded_key }, learn_redis_cb, { expanded_key, tostring(is_spam), symbol, tostring(is_unlearn), stat_tokens }) end @@ -136,7 +136,6 @@ exports.lua_bayes_init_statfile = function(classifier_ucl, statfile_ucl, symbol, rspamd_config:add_periodic(ev_base, 0.0, function(cfg, _) local function stat_redis_cb(err, data) - -- TODO: write this function lua_util.debugm(N, cfg, 'stat redis cb: %s, %s', err, data) if err then @@ -172,12 +171,54 @@ exports.lua_bayes_init_statfile = function(classifier_ucl, statfile_ucl, symbol, return gen_classify_functor(redis_params, classify_script_id), gen_learn_functor(redis_params, learn_script_id) end +local function gen_cache_check_functor(redis_params, check_script_id) + return function(task, cache_id, callback) + + local function classify_redis_cb(err, data) + lua_util.debugm(N, task, 'classify redis cb: %s, %s', err, data) + if err then + callback(task, false, err) + else + callback(task, true, data[1], data[2], data[3], data[4]) + end + end + + lua_redis.exec_redis_script(check_script_id, + { task = task, is_write = false, key = cache_id }, + classify_redis_cb, { cache_id }) + end +end + +local function gen_cache_learn_functor(redis_params, learn_script_id) + return function(task, cache_id, callback) + local function learn_redis_cb(err, data) + lua_util.debugm(N, task, 'learn_cache redis cb: %s, %s', err, data) + if err then + callback(task, false, err) + else + callback(task, true) + end + end + + lua_redis.exec_redis_script(learn_script_id, + { task = task, is_write = true, key = cache_id }, + learn_redis_cb, + { cache_id }) + + end +end + exports.lua_bayes_init_cache = function(classifier_ucl, statfile_ucl) local redis_params = load_redis_params(classifier_ucl, statfile_ucl) if not redis_params then return nil end + + local check_script_id = lua_redis.load_redis_script_from_file("bayes_cache_check.lua", redis_params) + local learn_script_id = lua_redis.load_redis_script_from_file("bayes_cache_learn.lua", redis_params) + + return gen_cache_check_functor(redis_params, check_script_id), gen_cache_learn_functor(redis_params, learn_script_id) end return exports |