|
1 | 1 | package caches |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "time" |
@@ -43,6 +44,15 @@ type Caches struct { |
43 | 44 | // dispatcher handles routing of invalidation events to all caches in this process. |
44 | 45 | // This is not exported as it's an internal implementation detail. |
45 | 46 | dispatcher *clustering.InvalidationDispatcher |
| 47 | + |
| 48 | + // registry maps cache resource names to invalidation handlers, used by the |
| 49 | + // internal cache invalidation endpoint. |
| 50 | + registry *middleware.InvalidationRegistry |
| 51 | +} |
| 52 | + |
| 53 | +// Invalidate removes the given keys from the named cache. |
| 54 | +func (c *Caches) Invalidate(ctx context.Context, cacheName string, keys []string) error { |
| 55 | + return c.registry.Invalidate(ctx, cacheName, keys) |
46 | 56 | } |
47 | 57 |
|
48 | 58 | // Close shuts down the caches and cleans up resources. |
@@ -175,6 +185,8 @@ func New(config Config) (Caches, error) { |
175 | 185 | }() |
176 | 186 | } |
177 | 187 |
|
| 188 | + registry := middleware.NewInvalidationRegistry() |
| 189 | + |
178 | 190 | ratelimitNamespace, err := createCache( |
179 | 191 | cache.Config[cache.ScopedKey, db.FindRatelimitNamespace]{ |
180 | 192 | Fresh: time.Minute, |
@@ -261,12 +273,13 @@ func New(config Config) (Caches, error) { |
261 | 273 |
|
262 | 274 | initialized = true |
263 | 275 | return Caches{ |
264 | | - RatelimitNamespace: middleware.WithTracing(ratelimitNamespace), |
265 | | - LiveApiByID: middleware.WithTracing(liveApiByID), |
266 | | - VerificationKeyByHash: middleware.WithTracing(verificationKeyByHash), |
267 | | - ClickhouseSetting: middleware.WithTracing(clickhouseSetting), |
268 | | - KeyAuthToApiRow: middleware.WithTracing(keyAuthToApiRow), |
269 | | - ApiToKeyAuthRow: middleware.WithTracing(apiToKeyAuthRow), |
| 276 | + RatelimitNamespace: middleware.WithInvalidation(middleware.WithTracing(ratelimitNamespace), "ratelimit_namespace", registry, cache.ScopedKeyFromString), |
| 277 | + LiveApiByID: middleware.WithInvalidation(middleware.WithTracing(liveApiByID), "live_api_by_id", registry, cache.ScopedKeyFromString), |
| 278 | + VerificationKeyByHash: middleware.WithInvalidation(middleware.WithTracing(verificationKeyByHash), "verification_key_by_hash", registry, middleware.StringKeyParser), |
| 279 | + ClickhouseSetting: middleware.WithInvalidation(middleware.WithTracing(clickhouseSetting), "clickhouse_setting", registry, middleware.StringKeyParser), |
| 280 | + KeyAuthToApiRow: middleware.WithInvalidation(middleware.WithTracing(keyAuthToApiRow), "key_auth_to_api_row", registry, cache.ScopedKeyFromString), |
| 281 | + ApiToKeyAuthRow: middleware.WithInvalidation(middleware.WithTracing(apiToKeyAuthRow), "api_to_key_auth_row", registry, cache.ScopedKeyFromString), |
270 | 282 | dispatcher: dispatcher, |
| 283 | + registry: registry, |
271 | 284 | }, nil |
272 | 285 | } |
0 commit comments