get is scoped to mcaptcha

This commit is contained in:
realaravinth 2021-06-06 11:51:51 +05:30
parent 184fe87e66
commit fd9b6f75ae
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
3 changed files with 31 additions and 19 deletions

View file

@ -91,7 +91,7 @@ impl Bucket {
/// creates new bucket and sets off timer to go off at `duration` /// creates new bucket and sets off timer to go off at `duration`
#[inline] #[inline]
pub fn new(ctx: &Context, duration: u64) -> CacheResult<Self> { fn new(ctx: &Context, duration: u64) -> CacheResult<Self> {
let decrement = HashMap::with_capacity(HIT_PER_SECOND); let decrement = HashMap::with_capacity(HIT_PER_SECOND);
let bucket_instant = get_bucket_instant(duration)?; let bucket_instant = get_bucket_instant(duration)?;
@ -111,7 +111,7 @@ impl Bucket {
/// increments count of key = captcha and registers for auto decrement /// increments count of key = captcha and registers for auto decrement
#[inline] #[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); let captcha_name = get_captcha_key(captcha);
ctx.log_debug(&captcha_name); ctx.log_debug(&captcha_name);
// increment // increment
@ -220,19 +220,7 @@ impl Bucket {
} }
} }
pub fn get(ctx: &Context, args: Vec<String>) -> RedisResult { /// Create new counter
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())
}
pub fn counter_create(ctx: &Context, args: Vec<String>) -> RedisResult { pub fn counter_create(ctx: &Context, args: Vec<String>) -> RedisResult {
let mut args = args.into_iter().skip(1); let mut args = args.into_iter().skip(1);
// mcaptcha captcha key name // mcaptcha captcha key name

View file

@ -25,6 +25,7 @@ mod mcaptcha;
mod utils; mod utils;
use bucket::MCAPTCHA_BUCKET_TYPE; use bucket::MCAPTCHA_BUCKET_TYPE;
use mcaptcha::MCAPTCHA_MCAPTCHA_TYPE;
/// Initial allocation ammount of bucket[bucket::Bucket] /// Initial allocation ammount of bucket[bucket::Bucket]
pub const HIT_PER_SECOND: usize = 100; pub const HIT_PER_SECOND: usize = 100;
@ -63,10 +64,10 @@ lazy_static! {
redis_module! { redis_module! {
name: "mcaptcha_cahce", name: "mcaptcha_cahce",
version: PKG_VERSION, version: PKG_VERSION,
data_types: [MCAPTCHA_BUCKET_TYPE,], data_types: [MCAPTCHA_BUCKET_TYPE, MCAPTCHA_MCAPTCHA_TYPE],
commands: [ commands: [
["mcaptcha_cache.count", bucket::Bucket::counter_create, "write", 1, 1, 1], ["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: [ event_handlers: [
[@EXPIRED @EVICTED: bucket::Bucket::on_delete], [@EXPIRED @EVICTED: bucket::Bucket::on_delete],

View file

@ -14,16 +14,23 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
//use redis_module::key::RedisKeyWritable;
use redis_module::native_types::RedisType; 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 redis_module::raw;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::bucket::Format; use crate::bucket::Format;
use crate::errors::*;
use crate::utils;
const REDIS_MCPATCHA_MCAPTCHA_TYPE_VERSION: i32 = 1; const REDIS_MCPATCHA_MCAPTCHA_TYPE_VERSION: i32 = 1;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct MCaptcha { pub struct MCaptcha {
m: libmcaptcha::MCaptcha, m: libmcaptcha::MCaptcha,
} }
@ -57,10 +64,26 @@ impl MCaptcha {
pub fn get_visitors(&self) -> u32 { pub fn get_visitors(&self) -> u32 {
self.m.get_visitors() self.m.get_visitors()
} }
/// Get counter value
pub fn get(ctx: &Context, args: Vec<String>) -> 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( pub static MCAPTCHA_MCAPTCHA_TYPE: RedisType = RedisType::new(
"mcaptmcapa", "mcaptmcap",
REDIS_MCPATCHA_MCAPTCHA_TYPE_VERSION, REDIS_MCPATCHA_MCAPTCHA_TYPE_VERSION,
raw::RedisModuleTypeMethods { raw::RedisModuleTypeMethods {
version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64, version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64,