-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support command and store function
- Loading branch information
1 parent
ea0a835
commit c09d28b
Showing
8 changed files
with
605 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::{ops::Deref, sync::Arc}; | ||
|
||
use dashmap::DashMap; | ||
|
||
use crate::RespFrame; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Backend(Arc<BackendInner>); | ||
|
||
// 使用 DashMap, 实现 Redis 存储 | ||
#[derive(Debug)] | ||
pub struct BackendInner { | ||
map: DashMap<String, RespFrame>, | ||
hmap: DashMap<String, DashMap<String, RespFrame>>, | ||
} | ||
|
||
impl Deref for Backend { | ||
type Target = BackendInner; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl BackendInner { | ||
pub fn new() -> Self { | ||
BackendInner { | ||
map: DashMap::new(), | ||
hmap: DashMap::new(), | ||
} | ||
} | ||
} | ||
impl Default for Backend { | ||
fn default() -> Self { | ||
Self(Arc::new(BackendInner::default())) | ||
} | ||
} | ||
impl Default for BackendInner { | ||
fn default() -> Self { | ||
BackendInner { | ||
map: DashMap::new(), | ||
hmap: DashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Backend { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
pub fn get(&self, key: &str) -> Option<RespFrame> { | ||
self.map.get(key).map(|v| v.value().clone()) | ||
} | ||
pub fn set(&self, key: String, value: RespFrame) { | ||
self.map.insert(key, value); | ||
} | ||
pub fn hget(&self, key: &str, field: &str) -> Option<RespFrame> { | ||
self.hmap | ||
.get(key) | ||
.and_then(|v| v.get(field).map(|v| v.value().clone())) | ||
} | ||
pub fn hset(&mut self, key: String, field: String, value: RespFrame) { | ||
let hmap = self.hmap.entry(key).or_default(); | ||
hmap.insert(field, value); | ||
} | ||
pub fn hgetall(&self, key: &str) -> Option<DashMap<String, RespFrame>> { | ||
self.hmap.get(key).map(|v| v.clone()) | ||
} | ||
} |
Oops, something went wrong.