spring redis 缓存
来源:互联网 发布:数据结构与算法百度云 编辑:程序博客网 时间:2024/06/17 04:55
上次已经学会通过在命令行的模式下使用redis,由于工作需求需要给 spring mvc 换缓存,ehcache 换成redis缓存。话不多说,下面是一些工作中的总结 引入spring 包以及junitredis包 jedis-2.8.0.jar spring-data-redis-1.7.2.RELEASE.jar
spring 配置
<context:component-scan base-package="com.test.cache" /> <cache:annotation-driven /> <!--序列化方式--> <bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean> <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> <cache:annotation-driven cache-manager="cacheManager" /> <!--工厂--> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost" /> <!--数据库编号 默认为 0 配置文件中默认总共为16个--> <property name="port" value="6379" /> <property name="database" value="8" /> <property name="usePool" value="true" /> <!-- <property name="password" value="123456" /> 如果redis配置文件中设置了密码就需要添加这个 --> <property name="timeout" value="100" /> <!--链接池--> <!-- <property name="poolConfig" ref="poolConfig" /> --> </bean> <!--模板--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultSerializer" ref="jdkSerializer" /> <property name="keySerializer" ref="stringSerializer" /> <!-- <property name="valueSerializer" ref="stringSerializer" /> <!--是否开启事务--> <!--<property name="enableTransactionSupport" value="true" />--> </bean> <!--spring缓存配置--> <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg ref="redisTemplate" /> <!--缓存默认时间--> <property name="defaultExpiration" value="1000" /> <!--是否开启前缀--> <property name="usePrefix" value="true" /> <!--缓存时间--> <property name="expires"> <map> <entry key="messageCache" value="30"></entry> </map> </property> </bean>
测试代码
public interface CacheDao{ String getMessage(String name); Person get(Person person);}
@Service( "helloService" )public class Cache implements CacheDao{ @Override /*@Transactional*/ @Cacheable( value = "messageCache", key = "#name" ) public String getMessage( String name ) { return "Hello" + name + "!"; } @Override @Cacheable(value="people",key="#person.id") public Person get(Person person){ System.out.println("hello"); return person; }}以上通过spring就是配置和缓存中的基本操纵 service中的 value="messageCache"会通过 配置中的 expires map来设置缓存存在时间, 即跟ehcache的ehcache.xml文件有些类似,这样方便我们对缓存的管理junit 代码@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = "classpath:applicationContext.xml" )public class JunitTest{ @Resource( name = "helloService" ) private CacheDao cacheDao; @Test public void test( ) { cacheDao.getMessage( "a" ); cacheDao.getMessage( "b" ); }}
运行结果
其中@Cacheable( value = "messageCache", key = "#name" )的key会制定具体名称
测试实体类缓存
@Test public void test( ) { Person person = new Person( ); person.setId( "8" ); person.setUserName( "Toney" ); person.setAge( "10" ); Cat cat = new Cat( ); cat.setId( "1" ); cat.setName( "Tom" ); person.setCat( cat ); List< String > list = new ArrayList< String >( ); list.add( "a" ); list.add( "b" ); person.setList( list ); Map< String, Object > map = new HashMap< String, Object >( ); map.put( "打酱油", "李白" ); map.put( "起床不刷牙", "杜甫" ); person.setMap( map ); Set< String > set = new HashSet< String >( ); set.add( "阿基米德" ); set.add( "爱英斯坦" ); person.setSet( set ); cacheDao.get(person ); }
以上可以发现上面的messageCache信息已经过期 其中Person的内容为一些序列化信息,是因为配置文件中设置默认的 defaultSerializer 为jdkSerializer 在保存实体类时,实体类必须实现序列化接口 Serializable
1 0
- redis spring缓存配置
- spring + redis实现缓存
- Redis+Spring缓存实例
- Redis+Spring缓存实例
- spring redis 缓存
- spring redis缓存配置
- Redis 缓存 + Spring
- Spring整合Redis缓存
- Spring+redis缓存使用
- Spring集成redis缓存
- spring整合redis缓存
- Spring集成redis缓存
- Spring整合Redis作为缓存
- Spring AOP 整合Redis 缓存
- spring整合redis缓存配置
- spring+redis做数据缓存
- spring aop 做redis缓存
- Spring整合Redis作为缓存
- 该如何设置数据连接池的初始大小
- java 基础学习之线程--多线程并发及线程锁
- Hibernate各种主键生成策略与配置详解
- [转]浅谈TCP/IP网络编程中socket的行为
- 关于模运算的性质
- spring redis 缓存
- 一套Java程序员笔试题
- 软件开发生命周期及文档
- 【数学相关、规律】Codeforces 707C Pythagorean Triples
- Codeforces Round #368 (Div. 2)(B. Bakery 贪心)
- java.io.IOException Connection Reset by Peer 的原因是什么
- String、StringBuilder、StringBuffer比较
- codeforces 707C Pythagorean Triples
- Java经典算法题目面试题经常出现的