Skip to content

Commit 87f58a3

Browse files
authored
Server: remove CacheKey::PREFIX_CACHE, kv_def!, string_kv_def! (#992)
Removes the associated const `PREFIX_CACHE` from the `CacheKey` trait, and supporting macro APIs. This diff should be non-breaking in that the key strings should be the same as before, just without the API confusion. ## Motivation Formerly it was possible to specify an override for the prefix of cache keys via an optional 3rd arg to `kv_def!`, and `string_kv_def!`. The issue is, the override (or the default when omitted) are not included in the actual string form of the key unless you explicitly `format!()` it in there. This is to say, the following are equivalent: ```rust // specify the override, then manually format it in. kv_def!(KeyA, ValA, "MY_PREFIX") impl KeyA { pub fn new(x: &str) -> Self { Self(format!("{Self::PREFIX_CACHE}_{x}")) } } // no override, so `Self::PREFIX_CACHE` is whatever the default is, but // it doesn't matter... it's not included in the key string :( kv_def!(KeyA, ValA) impl KeyA { pub fn new(x: &str) -> Self { // Including the prefix here means this version of KeyA is equiv Self(format!("MY_PREFIX_{x}")) } } ``` The problem comes when you omit `Self::PREFIX_CACHE` in the key string itself. The assumption would be either the default prefix, or an override when specified, would be included in the string automatically, but it isn't. ## Solution Removing the confusing 3rd arg from the macros and eliminating the associated const should mean moving forward: "wysiwyg." If there's a prefix in the key string, that's the prefix. There's no need for defaults or overrides.
2 parents 4bdeee0 + 0cae099 commit 87f58a3

File tree

4 files changed

+9
-28
lines changed

4 files changed

+9
-28
lines changed

server/svix-server/src/core/cache/mod.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ pub enum Error {
3838
type Result<T> = std::result::Result<T, Error>;
3939

4040
/// A valid key value for the cache -- usually just a wrapper around a [`String`]
41-
pub trait CacheKey: AsRef<str> + Send + Sync {
42-
const PREFIX_CACHE: &'static str = "SVIX_CACHE";
43-
}
44-
/// Any (de)serializable structure usuable as a value in the cache -- it is associated with a
41+
pub trait CacheKey: AsRef<str> + Send + Sync {}
42+
43+
/// Any (de)serializable structure usable as a value in the cache -- it is associated with a
4544
/// given key type to ensure type checking on creation or reading of values from the cache
4645
pub trait CacheValue: DeserializeOwned + Serialize + Send + Sync {
4746
type Key: CacheKey;
@@ -74,14 +73,6 @@ pub(crate) use kv_def_inner;
7473
/// A macro that creates a [`CacheKey`] and ties it to any value that implements
7574
/// [`DeserializeOwned`] and [`Serialize`]
7675
macro_rules! kv_def {
77-
($key_id:ident, $val_struct:ident, $lit_prefix:literal) => {
78-
crate::core::cache::kv_def_inner!($key_id, $val_struct);
79-
80-
impl CacheKey for $key_id {
81-
const PREFIX_CACHE: &'static str = $lit_prefix;
82-
}
83-
};
84-
8576
($key_id:ident, $val_struct:ident) => {
8677
crate::core::cache::kv_def_inner!($key_id, $val_struct);
8778

@@ -115,14 +106,6 @@ pub(crate) use string_kv_def_inner;
115106
// Used downstream and for testing:
116107
#[allow(unused_macros)]
117108
macro_rules! string_kv_def {
118-
($key_id:ident, $val_struct:ident, $lit_prefix:literal) => {
119-
crate::core::cache::string_kv_def_inner!($key_id, $val_struct);
120-
121-
impl CacheKey for $key_id {
122-
const PREFIX_CACHE: &'static str = $lit_prefix;
123-
}
124-
};
125-
126109
($key_id:ident, $val_struct:ident) => {
127110
crate::core::cache::string_kv_def_inner!($key_id, $val_struct);
128111

server/svix-server/src/core/idempotency.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ enum SerializedResponse {
5252
},
5353
}
5454

55-
kv_def!(IdempotencyKey, SerializedResponse, "SVIX_IDEMPOTENCY_CACHE");
55+
kv_def!(IdempotencyKey, SerializedResponse);
5656

5757
impl IdempotencyKey {
5858
fn new(auth_token: &str, key: &str, url: &str) -> IdempotencyKey {
@@ -65,6 +65,7 @@ impl IdempotencyKey {
6565
hasher.update(url);
6666

6767
let res = hasher.finalize();
68+
// FIXME: add (previously omitted) prefix: `SVIX_IDEMPOTENCY_CACHE`
6869
IdempotencyKey(base64::encode(res))
6970
}
7071
}

server/svix-server/src/core/message_app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl AppEndpointKey {
214214
// FIXME: Rewrite doc comment when AppEndpointValue members are known
215215
/// Returns a key for fetching all cached endpoints for a given organization and application.
216216
pub fn new(org: &OrganizationId, app: &ApplicationId) -> AppEndpointKey {
217-
AppEndpointKey(format!("{}_APP_v3_{}_{}", Self::PREFIX_CACHE, org, app))
217+
AppEndpointKey(format!("SVIX_CACHE_APP_v3_{}_{}", org, app))
218218
}
219219
}
220220

server/svix-server/src/worker.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct FailureCacheValue {
7373
pub first_failure_at: DateTimeUtc,
7474
}
7575

76-
kv_def!(FailureCacheKey, FailureCacheValue, "SVIX_FAILURE_CACHE");
76+
kv_def!(FailureCacheKey, FailureCacheValue);
7777

7878
impl FailureCacheKey {
7979
pub fn new(
@@ -82,11 +82,8 @@ impl FailureCacheKey {
8282
endp_id: &EndpointId,
8383
) -> FailureCacheKey {
8484
FailureCacheKey(format!(
85-
"{}_{}_{}_{}",
86-
Self::PREFIX_CACHE,
87-
org_id,
88-
app_id,
89-
endp_id
85+
"SVIX_FAILURE_CACHE_{}_{}_{}",
86+
org_id, app_id, endp_id
9087
))
9188
}
9289
}

0 commit comments

Comments
 (0)