diff --git a/src/bucket.rs b/src/bucket.rs index 0d906d8..84ab92e 100644 --- a/src/bucket.rs +++ b/src/bucket.rs @@ -91,7 +91,7 @@ impl Bucket { /// creates new bucket and sets off timer to go off at `duration` #[inline] - pub fn new(ctx: &Context, duration: u64) -> CacheResult { + fn new(ctx: &Context, duration: u64) -> CacheResult { let decrement = HashMap::with_capacity(HIT_PER_SECOND); let bucket_instant = get_bucket_instant(duration)?; @@ -111,7 +111,7 @@ impl Bucket { /// increments count of key = captcha and registers for auto decrement #[inline] - pub fn increment(ctx: &Context, duration: u64, captcha: &str) -> CacheResult<()> { + fn increment(ctx: &Context, duration: u64, captcha: &str) -> CacheResult<()> { let captcha_name = get_captcha_key(captcha); ctx.log_debug(&captcha_name); // increment @@ -220,19 +220,7 @@ impl Bucket { } } - pub fn get(ctx: &Context, args: Vec) -> RedisResult { - let mut args = args.into_iter().skip(1); - let key_name = args.next_string()?; - let key_name = utils::get_captcha_key(&key_name); - - let stored_captcha = ctx.open_key(&key_name); - if stored_captcha.key_type() == KeyType::Empty { - return errors::CacheError::new(format!("key {} not found", key_name)).into(); - } - - Ok(stored_captcha.read()?.unwrap().into()) - } - + /// Create new counter pub fn counter_create(ctx: &Context, args: Vec) -> RedisResult { let mut args = args.into_iter().skip(1); // mcaptcha captcha key name diff --git a/src/lib.rs b/src/lib.rs index 2a486c0..4b3ca5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ mod mcaptcha; mod utils; use bucket::MCAPTCHA_BUCKET_TYPE; +use mcaptcha::MCAPTCHA_MCAPTCHA_TYPE; /// Initial allocation ammount of bucket[bucket::Bucket] pub const HIT_PER_SECOND: usize = 100; @@ -63,10 +64,10 @@ lazy_static! { redis_module! { name: "mcaptcha_cahce", version: PKG_VERSION, - data_types: [MCAPTCHA_BUCKET_TYPE,], + data_types: [MCAPTCHA_BUCKET_TYPE, MCAPTCHA_MCAPTCHA_TYPE], commands: [ ["mcaptcha_cache.count", bucket::Bucket::counter_create, "write", 1, 1, 1], - ["mcaptcha_cache.get", bucket::Bucket::get, "readonly", 1, 1, 1], + ["mcaptcha_cache.get", mcaptcha::MCaptcha::get, "readonly", 1, 1, 1], ], event_handlers: [ [@EXPIRED @EVICTED: bucket::Bucket::on_delete], diff --git a/src/mcaptcha.rs b/src/mcaptcha.rs index 6c64b1c..f22b89f 100644 --- a/src/mcaptcha.rs +++ b/src/mcaptcha.rs @@ -14,16 +14,23 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +//use redis_module::key::RedisKeyWritable; use redis_module::native_types::RedisType; +use redis_module::raw::KeyType; +use redis_module::{Context, RedisResult}; +//use redis_module::RedisError; use redis_module::raw; + use serde::{Deserialize, Serialize}; use crate::bucket::Format; +use crate::errors::*; +use crate::utils; const REDIS_MCPATCHA_MCAPTCHA_TYPE_VERSION: i32 = 1; #[derive(Serialize, Deserialize)] -struct MCaptcha { +pub struct MCaptcha { m: libmcaptcha::MCaptcha, } @@ -57,10 +64,26 @@ impl MCaptcha { pub fn get_visitors(&self) -> u32 { self.m.get_visitors() } + + /// Get counter value + pub fn get(ctx: &Context, args: Vec) -> RedisResult { + use redis_module::NextArg; + + let mut args = args.into_iter().skip(1); + let key_name = args.next_string()?; + let key_name = utils::get_captcha_key(&key_name); + + let stored_captcha = ctx.open_key(&key_name); + if stored_captcha.key_type() == KeyType::Empty { + return CacheError::new(format!("key {} not found", key_name)).into(); + } + + Ok(stored_captcha.read()?.unwrap().into()) + } } pub static MCAPTCHA_MCAPTCHA_TYPE: RedisType = RedisType::new( - "mcaptmcapa", + "mcaptmcap", REDIS_MCPATCHA_MCAPTCHA_TYPE_VERSION, raw::RedisModuleTypeMethods { version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64,