Spring Cache

Spring 3.1 引入了缓存抽象层,使开发者能够透明地为现有应用添加缓存支持。类似于事务管理,缓存抽象层允许一致地使用各种缓存解决方案,同时对应用代码的影响降到最低。本文详细介绍 Spring 缓存抽象的概念、如何声明缓存策略及配置缓存等。

Cache Abstraction

35.1Instroduction

Since version 3.1 Spring Framework provides support for transparently adding caching into an existing Spring application.Similar to the transaction support, the caching abstraction allows consistent use of various caching solutions with minimal impact on the code.
spring3.1的时候支持了透明的添加缓存。想事物一样,缓存抽象了所有的缓存实现,然后提供统一的接口,这样对应用的代码产生最小的影响。
####Understanding the cache abstraction
Cache vs Buffer
The terms “buffer” and “cache” tend to be used interchangeably;note however they represent different things.A buffer is used traditionally as an intermediate temporary store for data between a fast and a slow entity. As one party would have to wait for the other affecting performance, the buffer alleviates this by allowing entire blocks of data to move aat once rathern then in small chucks.The data is written and read only once from the buffer.Furthermore, the buffers are visible to at least one party which is aware of it.
“buffer”和“cache”这两个属于人民趋向于相互使用。但注意,他们两是完全不同的东西。buffer常被用作高速io和低速io的临时性存储区域。其中一方不得不等待另一方,这时buffer就会允许将整块的数据只存储一次来减缓高速io等待的这种情况。当低速io准备好之后,在存储到低速io中。此外,buffer对于两方io中的至少一方是可见的。
spring cache的核心就是提供对方法的缓存。。每次目标方法被调用的时候,abstraction将会检查改方法是否对于给定的参数已经被执行过了。如果执行过了,这样就不会再执行方法,而是直接返回结果;如果给定参数对应的方法还没有被执行过,那么方法就会被执行,方法执行的结果就会被缓存并且返回给用户,下次方法被调用的时候就会直接返回缓存结果。采用这种方法,那些高消耗的方法(经常使用cpc或io)就会大大减少执行次数。缓存结果是透明提供的不需要调用任何接口。

important

Obviously this approach works only for methods that are guaranteed to return the same output(result)for a given input(or arguments )no matter how many times it is being executed.

很明显,缓存只适合那些不管输入多少次相同的参数,都会返回相同的结果的方法。
abstraction 还提供了其他操作,例如更新缓存中的内容,删除一个或者多个内容。在数据可变的应用程序中这些操作非常有用。
就想在springframework中的其他服务一样,缓存服务只是一层抽象,它还需要被提供真正的存储实现来对数据进行缓存,这样开发人员就不用写缓存逻辑,而只需要提供缓存实现就可以了。

有一下一些可以直接使用的cache abstraction的实现:jdk.util.concurrent.ConcurrentMap,EhCache,Gemfire cache,Guava caches 和JSR-107 compliant caches.

注意:

如果你有一个分布式环境(应用部署在多个节点上),你需要确认你的缓存厂商是否支持。对于不同的使用情况,你对于多个节点也许只保留了一份数据,但是当数据改变的时候,你需要一种传递机制,将更改的数据传递出去。

TO use the cache abstraction,the developer needs to take care of two aspects:
- caching declaration identify the methods that need to be cached and their policy
- cache configuration the backing cache where the data is stored and read from

使用cache abstraction 开发人员需要注意两个方面:
缓存申明,表明该方法需要被缓存和方法的缓存策略
对缓存进行配置,数据应当被存储在哪里,并在哪里被读取。

Declarative annotation-based caching

For caching declaration,the abstractionprovides a set of Java annotation:
- @Cacheable triggers cache population
- @CacheEvict triggers cache eviction
- @CachePut updates the cache without interfering with the method execution
- @Caching regroups multiple cache operations bo be applied on a method
- @CacheConfig shares some common cache-related settings at class-level
Let us take a closer look at each annotation:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值