-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheDistributionExample.cs
More file actions
41 lines (30 loc) · 1.39 KB
/
Copy pathCacheDistributionExample.cs
File metadata and controls
41 lines (30 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using PawSharp.Cache.Distribution;
using PawSharp.Cache.Providers;
using StackExchange.Redis;
// Example: Cache Distribution with Redis Pub/Sub
// Create Redis connection
var redis = ConnectionMultiplexer.Connect("localhost:6379");
// Create cache distributor
var distributor = new RedisCacheDistributor(redis, "pawsharp:cache");
// Create a cache provider (can be MemoryCacheProvider or RedisCacheProvider)
var memoryCache = new MemoryCacheProvider();
// Wrap the cache provider with distributed support
var distributedCache = new DistributedCacheProvider(memoryCache, distributor);
// Use the distributed cache like any other cache provider
// Cache invalidations are automatically propagated to all instances
distributedCache.CacheUser(user);
distributedCache.CacheGuild(guild);
// When you remove an entity, it's automatically invalidated across all instances
distributedCache.RemoveGuild(guildId); // This publishes invalidation to Redis
// Other instances listening on the same Redis channel will automatically
// invalidate their local cache when they receive the invalidation event
// The distributor also supports cache clear events
distributedCache.Clear(); // Publishes clear event to all instances
// Check if the distributor is healthy
if (distributor.IsHealthy())
{
Console.WriteLine("Cache distribution is healthy");
}
// Dispose when done
distributedCache.Dispose();
distributor.Dispose();