mirror of
https://github.com/mCaptcha/cache.git
synced 2024-11-24 17:35:31 +03:00
cluster redesigned with safe rust
This commit is contained in:
parent
8178c591ca
commit
05cd99597c
3 changed files with 19 additions and 43 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/target
|
||||
*.rdb
|
||||
tmp/
|
||||
|
|
50
src/lib.rs
50
src/lib.rs
|
@ -14,8 +14,6 @@
|
|||
* 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/>.
|
||||
*/
|
||||
use std::sync::Once;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use redis_module::raw::KeyType;
|
||||
|
@ -37,19 +35,18 @@ lazy_static! {
|
|||
let mut rng = rand::thread_rng();
|
||||
rng.gen()
|
||||
};
|
||||
|
||||
/// counter/captcha key prefix
|
||||
pub static ref PREFIX_COUNTER: String = format!("mcaptcha_cache:captcha:");
|
||||
|
||||
/// pocket key prefix
|
||||
pub static ref PREFIX_POCKET: String = format!("mcaptcha_cache:pocket:{{{}}}:", *ID);
|
||||
/// pocket timer key prefix
|
||||
pub static ref PREFIX_POCKET_TIMER: String = format!("mcaptcha_cache:timer:");
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// counter/captcha key prefix
|
||||
pub static mut PREFIX_COUNTER: &str = "mcaptcha_cache:captcha:";
|
||||
|
||||
/// pocket key prefix
|
||||
pub static mut PREFIX_POCKET: &str = "mcaptcha_cache:pocket:";
|
||||
|
||||
/// pocket timer key prefix
|
||||
pub static mut PREFIX_POCKET_TIMER: &str = "mcaptcha_cache:timer:";
|
||||
|
||||
static INIT_MODE: Once = Once::new();
|
||||
|
||||
/// If pockets perform clean up at x instant, then pockets themselves will get cleaned
|
||||
/// up at x + POCKET_EXPIRY_OFFSET(if they haven't already been cleaned up)
|
||||
pub const POCKET_EXPIRY_OFFSET: u64 = 30;
|
||||
|
@ -58,34 +55,14 @@ fn counter_create(ctx: &Context, args: Vec<String>) -> RedisResult {
|
|||
counter_runner(ctx, args)
|
||||
}
|
||||
|
||||
fn counter_cluster_create(ctx: &Context, args: Vec<String>) -> RedisResult {
|
||||
if !INIT_MODE.is_completed() {
|
||||
let id: usize = rand::random();
|
||||
unsafe {
|
||||
INIT_MODE.call_once(|| {
|
||||
//PREFIX_COUNTER = Box::leak(Box::new(format!("{}{{{}:node}}:", PREFIX_COUNTER, id)));
|
||||
PREFIX_POCKET = Box::leak(Box::new(format!("{}{{{}:node}}:", PREFIX_POCKET, id)));
|
||||
PREFIX_POCKET_TIMER =
|
||||
Box::leak(Box::new(format!("{}{{{}:node}}:", PREFIX_POCKET_TIMER, id)));
|
||||
});
|
||||
//j println!("{}", PREFIX_POCKET);
|
||||
//j println!("{}", PREFIX_POCKET_TIMER);
|
||||
//j return Ok(PREFIX_COUNTER.into());
|
||||
}
|
||||
}
|
||||
counter_runner(ctx, args)
|
||||
}
|
||||
|
||||
fn get(ctx: &Context, args: Vec<String>) -> RedisResult {
|
||||
let mut args = args.into_iter().skip(1);
|
||||
// mcaptcha captcha key name
|
||||
let key_name = args.next_string()?;
|
||||
ctx.log_warning(&key_name);
|
||||
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("key not found".into()).into();
|
||||
return errors::CacheError::new(format!("key {} not found", key_name)).into();
|
||||
}
|
||||
|
||||
Ok(stored_captcha.read()?.unwrap().into())
|
||||
|
@ -110,9 +87,8 @@ redis_module! {
|
|||
version: 1,
|
||||
data_types: [MCAPTCHA_POCKET_TYPE,],
|
||||
commands: [
|
||||
["mcaptcha_cache.count", counter_create, "write", 1, 2, 1],
|
||||
["mcaptcha_cache_cluster.count", counter_cluster_create, "write", 1, 1, 1],
|
||||
["mcaptcha_cache.get", get, "", 1, 1, 1],
|
||||
["mcaptcha_cache.count", counter_create, "write", 1, 1, 1],
|
||||
["mcaptcha_cache.get", get, "readonly", 1, 1, 1],
|
||||
],
|
||||
event_handlers: [
|
||||
[@EXPIRED @EVICTED: pocket::Pocket::on_delete],
|
||||
|
|
11
src/utils.rs
11
src/utils.rs
|
@ -23,19 +23,19 @@ use crate::*;
|
|||
#[inline]
|
||||
/// duration in seconds
|
||||
pub fn get_pocket_name(pocket_instant: u64) -> String {
|
||||
unsafe { format!("{}{}", PREFIX_POCKET, pocket_instant) }
|
||||
format!("{}{}", &*PREFIX_POCKET, pocket_instant)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// duration in seconds
|
||||
pub fn get_timer_name_from_pocket_name(pocket_name: &str) -> String {
|
||||
unsafe { format!("{}{}", PREFIX_POCKET_TIMER, pocket_name) }
|
||||
format!("{}{}", &*PREFIX_POCKET_TIMER, pocket_name)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// duration in seconds
|
||||
pub fn get_pocket_name_from_timer_name(name: &str) -> Option<&str> {
|
||||
unsafe { name.strip_prefix(&PREFIX_POCKET_TIMER) }
|
||||
name.strip_prefix(&*PREFIX_POCKET_TIMER)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -48,12 +48,12 @@ pub fn get_pocket_instant(duration: u64) -> Result<u64, RedisError> {
|
|||
|
||||
#[inline]
|
||||
pub fn get_captcha_key(name: &str) -> String {
|
||||
unsafe { format!("{}{}", PREFIX_COUNTER, name) }
|
||||
format!("{}{}", &*PREFIX_COUNTER, name)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_pocket_timer(name: &str) -> bool {
|
||||
unsafe { name.contains(&PREFIX_POCKET_TIMER) }
|
||||
name.contains(&*PREFIX_POCKET_TIMER)
|
||||
}
|
||||
|
||||
//#[cfg(test)]
|
||||
|
@ -82,7 +82,6 @@ mod temp {
|
|||
};
|
||||
|
||||
|
||||
|
||||
/// counter/captcha key prefix
|
||||
static ref PREFIX_COUNTER: String = format!("mcaptcha_cache:captcha:");
|
||||
|
||||
|
|
Loading…
Reference in a new issue