每次一条缓存记录被删除或更新的时候,Cache Manager会发送一个消息,让BackPlate存储这次的数据变化信息。所有其它的系统将异步接收这些消息,并将相应地作出更新和删除操作,保证数据的一致性。
五,ExpirationMode和CacheUpdateMode涉及到缓存,就必然有缓存过期的问题。CacheManager中提供了一些简单的缓存过期方式设置。
ExpirationMode 2 { 3 None = 0, 4 Sliding = 1, 5 Absolute = 2, 6 }
同时CacheManager还为多级缓存之间设置不同的数据更新策略
CacheUpdateMode 2 { 3 None = 0, 4 Full = 1, 5 Up = 2, 6 }
使用Sliding和Up, 我们我可以为多级缓存设置不同的缓存过期时间,这样使用频率高的数据就能够保存在访问速度更快的内存中,访问频率次高的放到分布式缓存中。当CacheManager在内存中找不到缓存数据的时候,就会尝试在分布式缓存中找。找到后,根据Up设置,会再将该缓存数据保存到内存缓存中。
具体的配置方式如下:
, settings => 2 { 3 settings.WithUpdateMode(CacheUpdateMode.Up) ).WithExpiration(ExpirationMode.Sliding, TimeSpan.FromSeconds(60))) 6 .And , config => { 9 config.WithAllowAdmin() 10 .WithDatabase(0) , 6379); 12 }). 13 .WithExpiration(ExpirationMode.Sliding, TimeSpan. FromHours (24))) .WithRetryTimeout(.WithRedisBackPlate().WithRedisCacheHandle(, });
六,缓存使用分析在缓存使用中,对于缓存hit和miss数据态比较关系,这些数据能够帮助我们分析和调整缓存的设置,帮助缓存使用地更加合理。
, settings => settings ) 3 .EnableStatistics() 4 .EnablePerformanceCounters());
在配置好缓存的Statistic功能后,我们就能够跟踪到缓存的使用情况了, 下面就是分别打印各个缓存handle中的分析数据。
1 foreach (var handle in cache.CacheHandles) 2 { 3 var stats = handle.Stats; 4 Console.WriteLine(string.Format( , 6 stats.GetStatistic(CacheStatsCounterType.Items), 7 stats.GetStatistic(CacheStatsCounterType.Hits), 8 stats.GetStatistic(CacheStatsCounterType.Misses), 9 stats.GetStatistic(CacheStatsCounterType.RemoveCalls), 10 stats.GetStatistic(CacheStatsCounterType.ClearRegionCalls), 11 stats.GetStatistic(CacheStatsCounterType.ClearCalls), 12 stats.GetStatistic(CacheStatsCounterType.AddCalls), 13 stats.GetStatistic(CacheStatsCounterType.PutCalls), 14 stats.GetStatistic(CacheStatsCounterType.GetCalls) 15 )); 16 }
七,结尾缓存是个好东西,用好了能够极大的提高性能。缓存的使用本身是个很大的话题,这边文章只是从缓存管理这个角度介绍了CachManager的使用。
下面是CacheManager相关的资料和链接:
官方主页
源代码
https://github.com/MichaCo/CacheManager
官方MVC项目的Sample
https://github.com/MichaCo/CacheManager/tree/master/samples/CacheManager.Samples.Mvc
最近在思考不同情况下缓存使用的区别问题。对于互联网项目来说,数据的一致性要求常常不太高,缓存管理中,关注点可能在缓存的命中率上。对于应用系统,访问请求不大,但是对于数据的一致性要求较高,缓存中的数据更新策略可能更加重要。
怎样才是好的适合应用系统的缓存设计呢? 如果大家有兴趣,欢迎探讨指教。