博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二)spring 集成 ehcache jgroups 集群
阅读量:5363 次
发布时间:2019-06-15

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

依赖

org.springframework
spring-context
4.1.6.RELEASE
org.springframework
spring-context-support
4.1.6.RELEASE
net.sf.ehcache
ehcache-core
2.6.11
net.sf.ehcache
ehcache-jgroupsreplication
1.7
org.springframework
spring-test
4.1.6.RELEASE
test
junit
junit
4.12
test

 

项目结构

│  pom.xml│├─src│  ├─main│  │  ├─java│  │  │  └─cn│  │  │      └─zno│  │  │              Person.java│  │  │              PersonService.java│  │  │              PersonServiceImpl.java│  │  ││  │  └─resources│  │          applicationContext-Ehcache.xml│  │          ehcache.xml│  ││  └─test│      ├─java│      │  └─ehcache│      │          TestEhcache.java│      ││      └─resources

 

applicationContext-Ehcache.xml

 

ehcache.xml (详细解释请看jgroups)

 

bean 及 服务类:

package cn.zno;public class Person {    private int age;    private Car car;    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public Car getCar() {        return car;    }    public void setCar(Car car) {        this.car = car;    }    @Override    public String toString() {        return "Person [age=" + age + ", car=" + car + "]";    }}class Car {    private String brand;    public String getBrand() {        return brand;    }    public void setBrand(String brand) {        this.brand = brand;    }    @Override    public String toString() {        return "Car [brand=" + brand + "]";    }}

 

PersonService.java

package cn.zno;public interface PersonService {        public Person getPerson();    public void cleanPersonCache();}

 

PersonServiceImpl.java

package cn.zno;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Servicepublic class PersonServiceImpl implements PersonService {    @Cacheable(value = "personCache", key = "123456")     public Person getPerson(){        try {            Thread.sleep(10000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        Person person = new Person();        Car car = new Car();        car.setBrand("benz");        person.setAge(11);        person.setCar(car);        return person;    }    @CacheEvict(value = "personCache", key = "123456")      public void cleanPersonCache() {        System.out.println("clean personCache[123456]");    }    }

 

测试类:

package ehcache;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.zno.PersonService;@ContextConfiguration(locations = { "classpath:applicationContext-Ehcache.xml" })@RunWith(SpringJUnit4ClassRunner.class)public class TestEhcache {    @Autowired    private CacheManager cacheManager;    @Autowired    private PersonService personService;    @Test    public void show() {        // 未缓存时耗时        long t1 = System.currentTimeMillis();        System.out.println(personService.getPerson().toString());        long t2 = System.currentTimeMillis();        System.out.println("耗时:" + (t2 - t1));        // 缓存后耗时        long t3 = System.currentTimeMillis();        System.out.println(personService.getPerson().toString());        long t4 = System.currentTimeMillis();        System.out.println("耗时:" + (t4 - t3));        // 清除缓存后耗时        long t5 = System.currentTimeMillis();        personService.cleanPersonCache();        System.out.println(personService.getPerson().toString());        long t6 = System.currentTimeMillis();        System.out.println("耗时:" + (t6 - t5));        // 再次获取        long t7 = System.currentTimeMillis();        System.out.println(personService.getPerson().toString());        long t8 = System.currentTimeMillis();        System.out.println("耗时:" + (t8 - t7));    }        @Test    public void create(){        cacheManager.addCache("testCache");        Cache cache = cacheManager.getCache("testCache");        cache.put(new Element("name", "xiaoming"));        System.out.println(cache.get("name"));    }}

 

show()运行结果:

十月 07, 2015 11:03:22 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]十月 07, 2015 11:03:22 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]十月 07, 2015 11:03:22 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners信息: Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]十月 07, 2015 11:03:22 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners信息: Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/TransactionDefinition]十月 07, 2015 11:03:22 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners信息: Using TestExecutionListeners: [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@15a191e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@270664]十月 07, 2015 11:03:22 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext-Ehcache.xml]十月 07, 2015 11:03:22 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.GenericApplicationContext@129df79: startup date [Wed Oct 07 11:03:22 CST 2015]; root of context hierarchy十月 07, 2015 11:03:23 上午 org.springframework.cache.ehcache.EhCacheManagerFactoryBean afterPropertiesSet信息: Initializing EhCache CacheManagerSLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.-------------------------------------------------------------------GMS: address=pc012-16440, cluster=EH_CACHE, physical address=172.16.162.238:13451-------------------------------------------------------------------Person [age=11, car=Car [brand=benz]]耗时:10031Person [age=11, car=Car [brand=benz]]耗时:0clean personCache[123456]Person [age=11, car=Car [brand=benz]]耗时:10000Person [age=11, car=Car [brand=benz]]耗时:0

 

create()运行结果:

-------------------------------------------------------------------GMS: address=pc012-40063, cluster=EH_CACHE, physical address=172.16.162.238:13550-------------------------------------------------------------------[ key = name, value=xiaoming, version=1, hitCount=1, CreationTime = 1444187930777, LastAccessTime = 1444187930777 ]

 

小结:

@Cacheable 的value是缓存名,key是在缓存中提取被缓存内容的key

<defaultCache  的作用是模板,不能通过default取出该缓存

<cache 可以配置到ehcache.xml 配置文件中,也可以在代码中手动添加(以<defaultCache 为模板)

mcast_addr 是组播地址,详细请查看jgroups

 

项目地址:git@github.com:witaste/ehcache.git

转载于:https://www.cnblogs.com/zno2/p/4858245.html

你可能感兴趣的文章
zoj 1232 Adventure of Super Mario
查看>>
组合数学 UVa 11538 Chess Queen
查看>>
Redis常用命令
查看>>
[转载]电脑小绝技
查看>>
thinkphp如何实现伪静态
查看>>
BZOJ 1925: [Sdoi2010]地精部落( dp )
查看>>
c++中的string常用函数用法总结!
查看>>
Week03-面向对象入门
查看>>
一个控制台程序,模拟机器人对话
查看>>
Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇——纯前端多页面)
查看>>
我的PHP学习之路
查看>>
【题解】luogu p2340 奶牛会展
查看>>
解决响应式布局下兼容性的问题
查看>>
使用DBCP连接池对连接进行管理
查看>>
【洛谷】【堆+模拟】P2278 操作系统
查看>>
hdu3307 欧拉函数
查看>>
Spring Bean InitializingBean和DisposableBean实例
查看>>
[容斥][dp][快速幂] Jzoj P5862 孤独
查看>>
软件开发工作模型
查看>>
Java基础之字符串匹配大全
查看>>