博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何理解Spring对缓存的支持
阅读量:6981 次
发布时间:2019-06-27

本文共 7894 字,大约阅读时间需要 26 分钟。

1.Spring缓存机制的理解

在Spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果;2.在某个方法执行前或后清空缓存。

下面写两个类来模拟Spring的缓存机制:

package com.sin90lzc.java.test;/** * 一个简单的Dao接口,我们要对这个接口的方法提供缓存的功能 * @author Tim * */public interface Dao {    Object select();    void save(Object obj);}
package com.sin90lzc.java.test;import java.util.HashMap;import java.util.Map;/** * 实现缓存功能,在Spring中,该类可以看作是Advice * @author Tim * */public class Cache {    private static final Map
cache = new HashMap
(); /** * 把对象添加到缓存中去 * @param key * @param value */ public void checkIn(String key, Object value) { if (!cache.containsKey(key)) { cache.put(key, value); } } /** * 从缓存中找对象 * @param key * @return */ public Object checkOut(String key) { if (cache.containsKey(key)) { return cache.get(key); } return null; } /** * 在方法执行前清除缓存 */ public void clearCacheBeforeMethod(){ cache.clear(); } /** * 在方法执行后清除缓存 */ public void clearCacheAfterMethod(){ cache.clear(); }}
package com.sin90lzc.java.test;/** * Dao的代理对象,它不仅仅完成主要的查询,存储操作,还实现了缓存 * @author Tim * */public class DaoProxy implements Dao {    private Cache cache;//该对象实现了缓存的功能    private Dao daoImpl;//完成主要查询,存储操作的Dao实现类,注意,这是一个Dao的实现类    public Object select() {        //在执行方法前,尝试从缓存中取出结果并返回,如果没找到,则执行实际的操作。        Object obj = cache.checkOut("DaoProxy.select");        boolean hasCache = false;        if (obj != null) {            hasCache = true;            return obj;        }                //实际的查询操作        obj = daoImpl.select();                //如果在缓存中找不到该方法的结果,缓存该结果        if (!hasCache) {            cache.checkIn("DaoProxy.select", obj);        }        return obj;    }    @Override    public void save(Object obj) {        //在执行方法前清除缓存        cache.clearCacheBeforeMethod();        //实际的操作        daoImpl.save(obj);        //在执行方法后清除缓存        cache.clearCacheAfterMethod();    }    public void setCache(Cache cache) {        this.cache = cache;    }    public void setDao(Dao dao) {        this.daoImpl = dao;    }}

从代码中可以看到,真正完成缓存功能的类是Cache,真正完成Dao(数据的增删查改)功能的类是Dao的实现类,这就是实现了实际业务(Dao)与功能(缓存)的分离。实际的Dao操作与缓存功能是如何结合起来的呢?这就是通过代理对象。我们可以注意到ProxyDao的真正身份也是一个Dao,是它把Dao操作与缓存结合起来的。这三个类恰恰说明了AOP的本质。

2.EHCache

Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能。其中,spring对EHCache提供了很好的支持。下面我们以EHCache为例来介绍spring的缓存配置。

在介绍Spring的缓存配置之前,我们先看一下EHCache是如何配置。

3.Spring缓存机制中的Advice

由于Spring的缓存机制是基于Spring的AOP,那么在Spring Cache中应该存在着一个Advice。没错,在Spring Cache中的Advice是存在的,它就是org.springframework.cache.Cache。我们看一下它的接口定义:

/* * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.cache;/** * Interface that defines the common cache operations. * * Note: Due to the generic use of caching, it is recommended that * implementations allow storage of null values (for example to * cache methods that return {
@code null}). * * @author Costin Leau * @since 3.1 */public interface Cache { /** * Return the cache name. */ String getName(); /** * Return the the underlying native cache provider. */ Object getNativeCache(); /** * Return the value to which this cache maps the specified key. Returns * null if the cache contains no mapping for this key. * @param key key whose associated value is to be returned. * @return the value to which this cache maps the specified key, * or null if the cache contains no mapping for this key */ ValueWrapper get(Object key); /** * Associate the specified value with the specified key in this cache. *

If the cache previously contained a mapping for this key, the old * value is replaced by the specified value. * @param key the key with which the specified value is to be associated * @param value the value to be associated with the specified key */ void put(Object key, Object value); /** * Evict the mapping for this key from this cache if it is present. * @param key the key whose mapping is to be removed from the cache */ void evict(Object key); /** * Remove all mappings from the cache. */ void clear(); /** * A (wrapper) object representing a cache value. */ interface ValueWrapper { /** * Return the actual value in the cache. */ Object get(); }}

evict,put方法就是Advice的功能方法,或者可以这样去理解。

但spring并不是直接使用org.springframework.cache.Cache,spring把Cache对象交给org.springframework.cache.CacheManager来管理,下面是org.springframework.cache.CacheManager接口的定义:

/* * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.cache;import java.util.Collection;/** * A manager for a set of {
@link Cache}s. * * @author Costin Leau * @since 3.1 */public interface CacheManager { /** * Return the cache associated with the given name. * @param name cache identifier (must not be {
@code null}) * @return associated cache, or {
@code null} if none is found */ Cache getCache(String name); /** * Return a collection of the caches known by this cache manager. * @return names of caches known by the cache manager. */ Collection
getCacheNames();}

在spring对EHCache的支持中,org.springframework.cache.ehcache.EhCacheManager就是org.springframework.cache.CacheManager的一个实现

  

 

4.基于xml配置方式配置缓存

在上一节中,我们得到了cacheManagr,那么我们就可以对某些方法配置缓存了。下面是基于xml方式的缓存配置

 

5.注解驱动的缓存配置

package com.sin90lzc.train.spring_cache.simulation;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component;@Componentpublic class DaoImpl implements Dao {    /**     * value定义了缓存区(缓存区的名字),每个缓存区可以看作是一个Map对象     * key作为该方法结果缓存的唯一标识,     */    @Cacheable(value = { "dao.select" },key="#id")    @Override    public Object select(int id) {        System.out.println("do in function select()");        return new Object();    }    @CacheEvict(value = { "dao.select" }, key="#obj")    @Override    public void save(Object obj) {        System.out.println("do in function save(obj)");    }}

转载于:https://www.cnblogs.com/sin90lzc/archive/2012/07/14/2587227.html

你可能感兴趣的文章
我的友情链接
查看>>
我的友情链接
查看>>
基础语法
查看>>
资质申报 - 系统集成企业资质等级评定条件(2012年修定版)
查看>>
在Linux下如何安装配置SVN服务
查看>>
你不得不知道的Visual Studio 2012(1)- 每日必用功能
查看>>
python3的数据类型以及模块的含义
查看>>
让你的输入框使用Google云语音输入技术
查看>>
vstpd服务
查看>>
跟阿里云技术专家阙寒一起深度了解视频直播CDN技术
查看>>
SQL SERVER中ROLLUP的用法
查看>>
配置DCOM中excel权限
查看>>
WEB SSH Ajaxterm客户端配置(1)
查看>>
FOSCommentBundle功能包:设置Doctrine ODM映射(投票)
查看>>
ondblog 修改informix日志模式
查看>>
ipvsadm命令参考
查看>>
网站排名下降的原因
查看>>
医院数据中心机房建设资料汇总(31篇)
查看>>
公司内部exchange2010 下删除误发邮件
查看>>
利用gevent实现异步执行任务
查看>>