从菜鸟的角度来学习Redis集成
1.一个在内存中的数据库(存储在内存中、速度极快)。
2.存储类型特别丰富,如常用的键值对存储等。
3.主从复制(可将任意类型的值复制到任意数量的从数据库中)。
…再详细的说明解释请去其它大神的博客下学习,下面我只将Redis缓存如何一步一步集成到Spring项目做详尽的说明(注解方式实现)
第一步jar包:spring-data-redis-1.6.2.RELEASE jedis-2.4.2.jar
1.注解驱动 <cache:annotation-driven cache-manager="cacheManager"/>
2.对象池:
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100"></property>
<property name="maxIdle" value="25"></property>
<property name="maxWaitMillis" value="15000"></property>
<property name="testOnBorrow" value="false"></property>
<property name="testOnReturn" value="false"></property>
</bean>
3.客户端连接工厂
<bean id="jedisConnectionFactory"
class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory'>
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="poolConfig"/>
<property name="usePool" value="true"/>
</bean>
4.模板类
<!-- redisTemplate redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref ="jedisConnectionFactory"/>
5.缓存管理器:使用redis 当做缓存
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" > <constructor-arg index="0" ref="redisTemplate"></constructor-arg> </bean>
1.一个在内存中的数据库(存储在内存中、速度极快)。
2.存储类型特别丰富,如常用的键值对存储等。
3.主从复制(可将任意类型的值复制到任意数量的从数据库中)。
…再详细的说明解释请去其它大神的博客下学习,下面我只将Redis缓存如何一步一步集成到Spring项目做详尽的说明(注解方式实现)
第一步jar包:spring-data-redis-1.6.2.RELEASE jedis-2.4.2.jar
第二步配置文件:
1.注解驱动 <cache:annotation-driven cache-manager="cacheManager"/>
2.对象池:
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100"></property>
<property name="maxIdle" value="25"></property>
<property name="maxWaitMillis" value="15000"></property>
<property name="testOnBorrow" value="false"></property>
<property name="testOnReturn" value="false"></property>
</bean>
3.客户端连接工厂
<bean id="jedisConnectionFactory"
class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory'>
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="poolConfig"/>
<property name="usePool" value="true"/>
</bean>
4.模板类
<!-- redisTemplate redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref ="jedisConnectionFactory"/>
5.缓存管理器:使用redis 当做缓存
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" > <constructor-arg index="0" ref="redisTemplate"></constructor-arg> </bean>
3.配置完成,可以在相关的方法上配置注解开始Redis缓存之旅啦^_^
例如:查询时使用,selectById
value:缓存区名称,key:在缓存区内对应的键,
表示查询缓存区“user”中key为参数id的缓存,如果没有则查询数据库,并把数据放入缓存中(注意这里缓存的数据是指方法执行完成返回的结果),以后直接从缓存取数据。
@Cacheable(key = "#id", value = "user")
简而言之方法上配置此注解及参数,查询时候首先查询去Redis缓存中寻找,如找到就使用缓存中的值,找不到则去库中查询!
类似的注解还有很多,抽时间我会单独整理一篇记录一下!如果Redis缓存配置出错可以给我留言大家一起努力解决,共同进步^_^