-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
269adc6
commit 39a88f1
Showing
28 changed files
with
565 additions
and
158 deletions.
There are no files selected for viewing
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
pub type PayloadLen = u64; | ||
|
||
pub type Version = u16; | ||
pub type KeyLen = u64; | ||
pub type Expiration = u32; |
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
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
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,13 @@ | ||
[package] | ||
name = "memcrab-server" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
tokio = { workspace = true, features = ["full"] } | ||
memcrab-protocol = { version = "0.1.0", path = "../memcrab-protocol" } | ||
lru = "0.12.1" | ||
thiserror.workspace = true | ||
typed-builder = "0.18.1" |
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,18 @@ | ||
# memcrab-server | ||
|
||
```rs | ||
use memcrab_server::{Server, CacheCfg}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let gb = 2_usize.pow(30); | ||
let cfg = CacheCfg::builder() | ||
.segments(10) | ||
.max_bytesize(gb) | ||
.build(); | ||
|
||
let addr = "127.0.0.1:9900".parse().unwrap(); | ||
let server = Server::from(cfg); | ||
server.start(addr).await.unwrap(); | ||
} | ||
``` |
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,48 @@ | ||
use std::sync::Mutex; | ||
use typed_builder::TypedBuilder; | ||
|
||
use crate::cache::map::Map; | ||
|
||
use super::{MemLru, Value}; | ||
|
||
#[derive(TypedBuilder)] | ||
pub struct CacheCfg { | ||
segments: usize, | ||
#[builder(default=None, setter(strip_option))] | ||
max_len: Option<usize>, | ||
max_bytesize: usize, | ||
} | ||
|
||
impl CacheCfg { | ||
pub(super) fn map(self) -> Map<String, Value> { | ||
assert!(self.segments > 0); | ||
|
||
let new_segment = |max_bytesize: usize, max_len: Option<usize>| { | ||
Mutex::new(match max_len { | ||
Some(max_len) => { | ||
if max_len == 0 { | ||
MemLru::with_max_bytesize(max_bytesize) | ||
} else { | ||
MemLru::with_max_bytesize_and_max_len(max_bytesize, max_len) | ||
} | ||
} | ||
None => MemLru::with_max_bytesize(max_bytesize), | ||
}) | ||
}; | ||
|
||
let mut segments = Vec::with_capacity(self.segments); | ||
let segment = new_segment( | ||
self.max_bytesize / self.segments + self.max_bytesize % self.segments, | ||
self.max_len.map(|l| l / self.segments + l % self.segments), | ||
); | ||
segments.push(segment); | ||
for _ in 1..self.segments { | ||
let segment = new_segment( | ||
self.max_bytesize / self.segments, | ||
self.max_len.map(|l| l / self.segments), | ||
); | ||
segments.push(segment); | ||
} | ||
Map::from_segments(segments) | ||
} | ||
} |
Oops, something went wrong.