了解和使用滑动缓存 (Sliding Cache) |
您所在的位置:网站首页 › cache技术实现 › 了解和使用滑动缓存 (Sliding Cache) |
定义 滑动缓存是一种缓存技术,它涉及为每个缓存项设置生存时间 (TTL) 值并在每次访问项目时更新TTL。 举个例子: 给一个缓存项设置5分钟生存时间,如果5分钟内没有人访问它,那么它可能会从缓存中被清除(或者更新); 如果5分钟内它被访问了,就立即更新TTL,也就是如果 10:03 被访问了,那么这个缓存项会在 10:08 才会过期,如果10:07 又被访问了,那么TTL再次更新,10:12才会过期。 代码DEMO(C#)C# 的 MemoryCache 就已经有这种缓存策略。 using System.Runtime.Caching; namespace DemoSlidingCache; internal class Program { private static void Main(string[] args) { Demo(); } private static void Demo() { MemoryCache cache = new MemoryCache("MyCache"); CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromSeconds(5) }; cache.Add("favorite", "Panda", policy); Console.WriteLine(DateTime.UtcNow.ToLongTimeString() + " Add cache with 5s sliding expiration"); // Retrieve the item from the cache object value = cache.Get("favorite"); Console.WriteLine(DateTime.UtcNow.ToLongTimeString() + " Get favorite: " + value); Thread.Sleep(TimeSpan.FromSeconds(3)); value = cache.Get("favorite"); Console.WriteLine(DateTime.UtcNow.ToLongTimeString() + " Get favorite: " + value); Thread.Sleep(TimeSpan.FromSeconds(3)); value = cache.Get("favorite"); Console.WriteLine(DateTime.UtcNow.ToLongTimeString() + " Get favorite: " + value); Thread.Sleep(TimeSpan.FromSeconds(6)); value = cache.Get("favorite"); Console.WriteLine(DateTime.UtcNow.ToLongTimeString() + " Get favorite: " + (value ?? "null")); } }输出结果 每次访问都增加了缓存项 Panda 的过期时间,直到最后一次超过了5秒都没被访问,这个缓存项就被清除了。 简单实现 (C#)了解了定义,我们可以尝试自己实现一个简单的SlidingCache namespace DemoSlidingCache; public class SimpleSlidingCache { private Dictionary _cache; public SimpleSlidingCache() { _cache = new Dictionary(); } public object? Get(string key) { Clean(); SimpleCacheItem? cacheItem; if (_cache.TryGetValue(key, out cacheItem)) { if (cacheItem != null) { cacheItem.Renew(); return cacheItem.Value; } } return null; } public void Add(string key, object value, TimeSpan timeSpan) { Clean(); _cache[key] = new SimpleCacheItem(timeSpan, value); } private void Clean() { var keys = _cache.Keys.ToList(); foreach (string key in keys) { var cacheItem = _cache[key]; DateTime utcNow = DateTime.UtcNow; if (cacheItem.ExpireTime没有问题。 写一点 Unit Test using DemoSlidingCache; namespace SimpleSlidingCacheTest { public class SimpleSlidingCacheUnitTest { [Fact] public void AddAndRead() { SimpleSlidingCache cache = new SimpleSlidingCache(); cache.Add("key", "value", TimeSpan.FromSeconds(10)); Assert.Equal("value", cache.Get("key")); } [Fact] public void Expire() { SimpleSlidingCache cache = new SimpleSlidingCache(); cache.Add("key", "value", TimeSpan.FromSeconds(3)); Thread.Sleep(TimeSpan.FromSeconds(4)); Assert.Null(cache.Get("key")); } [Fact] public void Renew() { SimpleSlidingCache cache = new SimpleSlidingCache(); cache.Add("key", "value", TimeSpan.FromSeconds(3)); Thread.Sleep(TimeSpan.FromSeconds(2)); cache.Get("key"); Thread.Sleep(TimeSpan.FromSeconds(2)); Assert.Equal("value", cache.Get("key")); } } } 优缺点对比普通的设置TTL的缓存,滑动缓存的优缺点如下 优点: - 提升性能,降低服务器负载。通过减少访问服务器(或数据库等)的次数,并将经常使用的数据存储在本地,滑动缓存可以显著提高性能,减少响应时间,同时减少了服务器端系统的负载,提高了可扩展性和可靠性 缺点: - 复杂性增加:实施滑动缓存可能比其他缓存技术更复杂,特别是在管理大量的缓存项目时。 - 不准确的数据。如果缓存中的数据没有得到适当的管理,它可能会变得过时,导致不正确或陈旧的信息被显示给用户。 因此,滑动缓存的适用场景就呼之欲出了 —— 缓存拥有以下特点的数据 一些时间段内被频繁访问不常更新新数据和旧数据的差异对业务没有重大影响 —— 容许一定时间的更新延迟参考链接 ICacheEntry.SlidingExpiration Property (Microsoft.Extensions.Caching.Memory) CacheInsertOptions.SlidingExpiration Property (System.Web.Caching) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |