pub struct MemcachedCacheDriver { /* private fields */ }Expand description
Memcached 缓存驱动(对齐 PHP think\cache\driver\Memcached)
实现 CacheDriver trait,通过 MemcachedBackend 抽象层操作 Memcached。
§标签模拟
由于 Memcached 不支持 Set 数据结构,标签功能通过 JSON 序列化模拟:
tag_append(name, value):读取tag:<md5(tag)>的 JSON 数组 → 追加 → 写回tag_items(tag):读取tag:<md5(tag)>的 JSON 数组tag_clear(keys):逐个删除 key
§用法
ⓘ
use sz_rust_cache_facade::{Cache, MemcachedCacheDriver, MemcachedConfig, MockMemcachedBackend};
let cache = Cache::new();
cache.register_default(
MemcachedCacheDriver::with_backend(
MemcachedConfig::default(),
Box::new(MockMemcachedBackend::new()),
)
);
cache.set("key", "value", None).unwrap();
assert_eq!(cache.get::<String>("key").unwrap(), Some("value".to_string()));Implementations§
Source§impl MemcachedCacheDriver
impl MemcachedCacheDriver
Sourcepub fn new(config: MemcachedConfig) -> Self
pub fn new(config: MemcachedConfig) -> Self
创建 Memcached 缓存驱动(用 Mock backend)
对齐 PHP new \think\cache\driver\Memcached($options)。
Sourcepub fn with_backend(
config: MemcachedConfig,
backend: Box<dyn MemcachedBackend>,
) -> Self
pub fn with_backend( config: MemcachedConfig, backend: Box<dyn MemcachedBackend>, ) -> Self
创建 Memcached 缓存驱动(自定义 backend)
应用层可注入真实 Memcached backend(如 memcache::Client 包装)。
Sourcepub fn config(&self) -> &MemcachedConfig
pub fn config(&self) -> &MemcachedConfig
获取配置引用
Sourcepub fn append(&self, name: &str, value: &str) -> Result<(), CacheError>
pub fn append(&self, name: &str, value: &str) -> Result<(), CacheError>
追加 TagSet 数据(模拟 PHP Driver::append)
Memcached 不支持 Set,通过 JSON 序列化模拟:
- 读取
tag:<md5(tag)>的 JSON 数组(若不存在则为空数组) - 追加新 value(去重)
- 写回
Sourcepub fn get_tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError>
pub fn get_tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError>
获取标签包含的缓存标识(模拟 PHP getTagItems)
Trait Implementations§
Source§impl CacheDriver for MemcachedCacheDriver
impl CacheDriver for MemcachedCacheDriver
Source§fn set_raw(
&self,
key: &str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Result<(), CacheError>
fn set_raw( &self, key: &str, value: Vec<u8>, ttl: Option<Duration>, ) -> Result<(), CacheError>
写入缓存原始字节
Source§fn inc(&self, key: &str, step: i64) -> Result<i64, CacheError>
fn inc(&self, key: &str, step: i64) -> Result<i64, CacheError>
自增(对齐 PHP
inc,不经序列化层) Read moreSource§fn dec(&self, key: &str, step: i64) -> Result<i64, CacheError>
fn dec(&self, key: &str, step: i64) -> Result<i64, CacheError>
自减(对齐 PHP
dec,不经序列化层) Read moreSource§fn tag_append(&self, tag_key: &str, cache_key: &str) -> Result<(), CacheError>
fn tag_append(&self, tag_key: &str, cache_key: &str) -> Result<(), CacheError>
Auto Trait Implementations§
impl !RefUnwindSafe for MemcachedCacheDriver
impl !UnwindSafe for MemcachedCacheDriver
impl Freeze for MemcachedCacheDriver
impl Send for MemcachedCacheDriver
impl Sync for MemcachedCacheDriver
impl Unpin for MemcachedCacheDriver
impl UnsafeUnpin for MemcachedCacheDriver
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more