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

使用Cache容易遇到的问题:

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

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

解决方案

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

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

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

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

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

适合使用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();
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

Cache-Aside Pattern解析的更多相关文章

  1. Cache Aside Pattern

    Cache Aside Pattern 即旁路缓存是缓存方案的经验实践,这个实践又分读实践,写实践 对于读请求 先读cache,再读db 如果,cache hit,则直接返回数据 如果,cache m ...

  2. 缓存实践Cache Aside Pattern

    Cache Aside Pattern旁路缓存,是对缓存应用的一个总结,包括读数据方案和写数据方案. 读数据方案 先读cache,如果命中则返回 如果miss则读db 将db的数据存入缓存 写数据方案 ...

  3. Google guava cache源码解析1--构建缓存器(1)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.guava cache 当下最常用最简单的本地缓存 线程安全的本地缓存 类似于ConcurrentHas ...

  4. 第二章 Google guava cache源码解析1--构建缓存器

    1.guava cache 当下最常用最简单的本地缓存 线程安全的本地缓存 类似于ConcurrentHashMap(或者说成就是一个ConcurrentHashMap,只是在其上多添加了一些功能) ...

  5. CSAPP-Lab05 Cache Lab 深入解析

    本文首发于我的知乎专栏:https://zhuanlan.zhihu.com/p/484657229 实验概览 Cache Lab 分为两部分,编写一个高速缓存模拟器以及要求优化矩阵转置的核心函数,以 ...

  6. Guava Cache源码解析

    概述: 本次主要是分析cache的源码,基本概念官方简介即可. 基本类图: 在官方的文档说明中,Guava Cache实现了三种加载缓存的方式: LoadingCache在构建缓存的时候,使用buil ...

  7. Google guava cache源码解析1--构建缓存器(3)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 下面介绍在LocalCache(CacheBuilder, CacheLoader)中调用的一些方法: Ca ...

  8. Google guava cache源码解析1--构建缓存器(2)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. CacheBuilder-->maximumSize(long size)     /**       ...

  9. Spring Cache 源码解析

    这个类实现了Spring的缓存拦截器 org.springframework.cache.interceptor.CacheInterceptor @SuppressWarnings("se ...

随机推荐

  1. [转]关于jquery中html()、text()、val()的区别

    原文地址:http://www.cnblogs.com/xiaolifeidao/p/3715830.html .html()用为读取和修改元素的HTML标签    对应js中的innerHTML . ...

  2. mysql5.5的安装与配置(亲测版)

    mysql5.5.x的编译安装 说明:5.5和5.1差不多,只是在编译时增加了一个cmake,其他基本一样,mysql5.5默认编码是utf-8,在使用mysql5.5配置lnmp或者lamp后安装d ...

  3. Apache Shiro和Spring Security的详细对比

    参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...

  4. Maven-eclipse运行maven命令

    右击项目,点击Run as,如下图: 即可看到有很多现有的maven命令,点击即可运行,并在控制台可以看到运行信息 如果你想运行的maven命令在这里没有找到,点击Maven build创建新的命令, ...

  5. 【USACO 1.4】Mother's Milk

    /* TASK: milk3 LANG: C++ SOLVE: 倒水,dfs,枚举每一种倒法,ca[i][j]记录a和c桶的状态,因为总体积不变,故b的状态不需要记录. */ #include< ...

  6. 精通Web Analytics 2.0 (3) 第一章:网站分析的新奇世界

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第一章:Web Analytics 2.0的新奇世界 多年以来,我们很清楚的知道,网站分析能够真正的改革网络上业务的完成方式.那 ...

  7. Leetcode Move Zeros

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  8. exists的用法

    今天突然看到之前自己写的一个代码 久久没想通为毛.. 看来笔记还是需要 exists可以代替子查询in  比in 更高效   默认是exists 查询中包含有数据则条件成立..否则没数据 select ...

  9. phpweb /version.php Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Relevant Link:2. 漏洞触发条件3. 漏洞影响范围4. 漏 ...

  10. dedecms /include/uploadsafe.inc.php SQL Injection Via Local Variable Overriding Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 . dedecms原生提供一个"本地变量注册"的模拟 ...