zoukankan      html  css  js  c++  java
  • Cache-Aside Pattern解析

    使用这种模式,可以帮助我们维护Cache中的数据。

    使用Cache容易遇到的问题:

    使用缓存,主要是为了将一些重复访问的数据存到缓存,开发者希望缓存中的数据和数据源中的保持一致,这就需要程序中有相应的策略:

    • 如果缓存中的数据被更新了,能尽可能快的同步到数据源中
    • 如果数据源的数据被修改了,缓存的数据被更新或者删除

    解决方案

    一些商用的Cache会提供读穿透(read-through)和写穿透(write-through/write-behind)模式:

    • 读穿透:程序尝试从缓存中读取数据,如果缓存中没有相应的数据,则会继续从数据源中读取并将其加入到缓存中
    • 写穿透:如果尝试更新缓存中的数据,则后台的数据源也会被自动的更新

    通过实现Cache-Aside Pattern来模拟读穿透:

     Figure 1 - Using the Cache-Aside pattern to store data in the cache

    当程序更新了信息后,模拟写穿透功能:

    • 确保更新的数据存入到数据源中
    • 将缓存中的数据标记为无效

    适合使用Cache-Aside Pattern的场景:

    • 缓存没有提供读穿透和写穿透功能
    • 按需加载的数据不可预测

    可能不适合使用Cache-Aside Pattern的场景:

    • 缓存的对象是静态的(在程序启动的时候就在缓存中添加一个静态对象并长期有效)
    • Web应用缓存Session状态,在这种情况下,应该避免客户端和服务器的sticky session

    如果使用的Cahce没有提供这2种模式,需要我们在代码中实现,例子如下:

    通过Azure Cache,我们可以创建分布式的缓存集群,这样一个应用的多个实例都可以访问。

    GetMyEntityAsync 实现了读穿透模式:

    private DataCache cache;
    ...
    
    public async Task<MyEntity> GetMyEntityAsync(int id)
    {  
      // Define a unique key for this method and its parameters.
      var key = string.Format("StoreWithCache_GetAsync_{0}", id);
      var expiration = TimeSpan.FromMinutes(3);
      bool cacheException = false;
    
      try
      {
        // Try to get the entity from the cache.
        var cacheItem = cache.GetCacheItem(key);
        if (cacheItem != null)
        {
          return cacheItem.Value as MyEntity;
        }
      }
      catch (DataCacheException)
      {
        // If there is a cache related issue, raise an exception 
        // and avoid using the cache for the rest of the call.
        cacheException = true;
      }
    
      // If there is a cache miss, get the entity from the original store and cache it.
      // Code has been omitted because it is data store dependent.  
      var entity = ...;
    
      if (!cacheException)
      {
        try
        {
          // Avoid caching a null value.
          if (entity != null)
          {
            // Put the item in the cache with a custom expiration time that 
            // depends on how critical it might be to have stale data.
            cache.Put(key, entity, timeout: expiration);
          }
        }
        catch (DataCacheException)
        {
          // If there is a cache related issue, ignore it
          // and just return the entity.
        }
      }
    
      return entity;
    }

     UpdateEntityAsync 实现了写穿透模式:

    public async Task UpdateEntityAsync(MyEntity entity)
    {
      // Update the object in the original data store
      await this.store.UpdateEntityAsync(entity).ConfigureAwait(false);
    
      // Get the correct key for the cached object.
      var key = this.GetAsyncCacheKey(entity.Id);
    
      // Then, invalidate the current cache object
      this.cache.Remove(key);
    }
    
    private string GetAsyncCacheKey(int objectId)
    {
      return string.Format("StoreWithCache_GetAsync_{0}", objectId);
    }

    注意:操作的顺序很重要,一定是先更新数据源中的信息,在将Cache中的数据删除掉;如果先删除Cache的数据而后更新数据源,会有小概率发生尝试从缓存获取这个数据,因为它已经被删除掉了,而从数据源读出还没有被更新的旧数据并且将这个旧数据存放到了缓存中。

    如果想使用Azure Cache的API,可以参考Using Microsoft Azure Cache

  • 相关阅读:
    远程监控基础知识和故障排除
    感慨颇多
    物联网操作系统Hello China移植mile stone之一:移植基础版本V1.76发布
    iOS5系统API和5个开源库的JSON解析速度测试
    (译)iPhone: 用公开API创建带小数点的数字键盘 (OS 3.0, OS 4.0)
    [工具]Mac平台开发几个网络抓包工具(sniffer)
    [工具]Mac下非常好用的快捷终端Dterm
    【IOS】在SDK中打开其他接入应用的解决方案
    [开源]在iOS上实现Android风格的控件Toast
    [技巧]使用Xcode集成的HeaderDoc自动生成注释和开发文档
  • 原文地址:https://www.cnblogs.com/xiuj/p/4675368.html
Copyright © 2011-2022 走看看