@@ -40,6 +40,10 @@ type Result<T> = std::result::Result<T, Error>;
40
40
/// A valid key value for the cache -- usually just a wrapper around a [`String`]
41
41
pub trait CacheKey : AsRef < str > + Send + Sync { }
42
42
43
+ /// A cache key for setting/getting raw [`String`]s -- this is just a marker
44
+ /// trait added in the `string_kv_def macro`
45
+ pub trait StringCacheKey : AsRef < str > + Send + Sync { }
46
+
43
47
/// Any (de)serializable structure usable as a value in the cache -- it is associated with a
44
48
/// given key type to ensure type checking on creation or reading of values from the cache
45
49
pub trait CacheValue : DeserializeOwned + Serialize + Send + Sync {
@@ -85,7 +89,7 @@ pub(crate) use kv_def;
85
89
/// but it can't be made private or else it couldn't be used in the outer macro.
86
90
#[ allow( unused_macros) ]
87
91
macro_rules! string_kv_def_inner {
88
- ( $key_id: ident, $val_struct : ident ) => {
92
+ ( $key_id: ident) => {
89
93
#[ derive( Clone , Debug ) ]
90
94
pub struct $key_id( String ) ;
91
95
@@ -94,10 +98,6 @@ macro_rules! string_kv_def_inner {
94
98
& self . 0
95
99
}
96
100
}
97
-
98
- impl StringCacheValue for $val_struct {
99
- type Key = $key_id;
100
- }
101
101
} ;
102
102
}
103
103
#[ allow( unused_imports) ]
@@ -106,10 +106,12 @@ pub(crate) use string_kv_def_inner;
106
106
// Used downstream and for testing:
107
107
#[ allow( unused_macros) ]
108
108
macro_rules! string_kv_def {
109
- ( $key_id: ident, $val_struct : ident ) => {
110
- crate :: core:: cache:: string_kv_def_inner!( $key_id, $val_struct ) ;
109
+ ( $key_id: ident) => {
110
+ crate :: core:: cache:: string_kv_def_inner!( $key_id) ;
111
111
112
- impl CacheKey for $key_id { }
112
+ impl crate :: core:: cache:: StringCacheKey for $key_id { }
113
+ // so key can work w/ other methods, like delete:
114
+ impl crate :: core:: cache:: CacheKey for $key_id { }
113
115
} ;
114
116
}
115
117
#[ allow( unused_imports) ]
@@ -159,16 +161,12 @@ pub trait CacheBehavior: Sync + Send {
159
161
160
162
async fn get_raw ( & self , key : & [ u8 ] ) -> Result < Option < Vec < u8 > > > ;
161
163
162
- async fn get_string < T : StringCacheValue > ( & self , key : & T :: Key ) -> Result < Option < T > > {
164
+ async fn get_string < T : StringCacheKey > ( & self , key : & T ) -> Result < Option < String > > {
163
165
run_with_retries (
164
166
|| async move {
165
167
self . get_raw ( key. as_ref ( ) . as_bytes ( ) )
166
168
. await ?
167
- . map ( |x| {
168
- String :: from_utf8 ( x)
169
- . map_err ( |e| e. into ( ) )
170
- . and_then ( |x| x. try_into ( ) . map_err ( |_| Error :: DeserializationOther ) )
171
- } )
169
+ . map ( |x| String :: from_utf8 ( x) . map_err ( |e| e. into ( ) ) )
172
170
. transpose ( )
173
171
} ,
174
172
|e| self . should_retry ( e) ,
@@ -195,10 +193,10 @@ pub trait CacheBehavior: Sync + Send {
195
193
196
194
async fn set_raw ( & self , key : & [ u8 ] , value : & [ u8 ] , ttl : Duration ) -> Result < ( ) > ;
197
195
198
- async fn set_string < T : StringCacheValue > (
196
+ async fn set_string < T : StringCacheKey > (
199
197
& self ,
200
- key : & T :: Key ,
201
- value : & T ,
198
+ key : & T ,
199
+ value : & str ,
202
200
ttl : Duration ,
203
201
) -> Result < ( ) > {
204
202
run_with_retries (
@@ -237,10 +235,10 @@ pub trait CacheBehavior: Sync + Send {
237
235
238
236
async fn set_raw_if_not_exists ( & self , key : & [ u8 ] , value : & [ u8 ] , ttl : Duration ) -> Result < bool > ;
239
237
240
- async fn set_string_if_not_exists < T : StringCacheValue > (
238
+ async fn set_string_if_not_exists < T : StringCacheKey > (
241
239
& self ,
242
- key : & T :: Key ,
243
- value : & T ,
240
+ key : & T ,
241
+ value : & str ,
244
242
ttl : Duration ,
245
243
) -> Result < bool > {
246
244
run_with_retries (
0 commit comments