Redis简介
Redis是一个数据以key-value形式存储的NoSQL数据库,是基于内存的存储中间件,可用来做缓存、消息队列等。由于Redis是基于内存的,因此它的效率较高,若把它当缓存可以减轻数据库压力,提高查询效率。
传统的查询
加了Redis的查询
请求的时候先经过Redis查看是否存在数据,若不存在则去MySQL中获取,获取到数据则存储在Redis中,下次直接在Redis中获取。
开始前的说明
由于Spring是个成熟的框架,它为开发者提供了缓存的支持,因此使用起来及其方便,使用@Cacheable注解就可以实现。
Spring中提供了自己的缓存,但如果要使用Redis,则需要实现Spring的Cache接口才能使用,好在Spring中提供了实现了该接口的Redis实现类RedisCache,只需要做好相应的配置即可使用,甚至不用自己实现。
环境搭建
首先安装Reids,只在自己的服务器上用Docker创建Redis容器即可,当然也可以不用Docker,看个人习惯。
# 拉取 Redis 镜像
docker pull redis
# 创建 Redis 容器
docker run -d --name=redis -p 6379:6379 redis
复制
Redis环境搞定,接下来加项目代码中配置,先导入依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
<!-- Jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
复制
完事后创建一个redis-config.properties文件,写入Redis相关配置
# server IP
redis.host=你的IP
# server port
redis.port=6379
# server pass
redis.pass=你的密码
# use dbIndex
redis.database=0
# 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
redis.maxIdle=300
# 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;
redis.maxWait=3000
# 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
redis.testOnBorrow=true
复制
然后创建一个spring-redis.xml文件,写入如下配置(写好别忘了在web.xml中加载该文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">
<context:property-placeholder location="classpath:redis-config.properties" >
<cache:annotation-driven cache-manager="cacheManager" key-generator="workingKeyGenerator"/>
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" >
<property name="maxWaitMillis" value="${redis.maxWait}" >
<property name="testOnBorrow" value="${redis.testOnBorrow}" >
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<!-- RedisCache构造方法中的四个参数,name:缓存的名字,与注解中的属性有关 -->
<!-- redisOperations:redis相关操作 -->
<!-- expiration:缓存存活时间,以秒计 -->
<bean class="org.springframework.data.redis.cache.RedisCache">
<constructor-arg index="0" name="name" value="common"/>
<constructor-arg index="1">
<null/>
</constructor-arg>
<constructor-arg index="2" name="redisOperations" ref="redisTemplate"/>
<constructor-arg index="3" name="expiration" value="300"/>
</bean>
</set>
</property>
</bean>
<!-- key生成器 -->
<bean id="workingKeyGenerator" class="org.springframework.cache.interceptor.SimpleKeyGenerator"/>
</beans>
复制
这样就配置好了,接下来只需要在Service层的方法上加@Cacheable注解
/**
* value需要和配置文件里的一致
* key:key的生成策略,getMethodName():方法名,#pageNum:参数的值
* 方法中两个参数是用于PageHelper,我用AOP切入了因此没有其他代码
*/
@Cacheable(value = "common", key = "getMethodName()+'_'+#pageNum")
public Object findGoods(int pageNum, int pageSize){
return goodsDao.findGoods();
}
复制
运行结果
运行后可以看到Redis中已经存入了数据,那些符号是因为序列化的原因,但看第二个key的后面,与注解中定义的key一致,说明成功了。
文章转载自大数据的奇妙冒险,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
深入理解分布式锁:原理、应用与挑战
京东云开发者
40次阅读
2025-03-11 15:50:54
缓存监控治理在游戏业务的实践和探索
vivo互联网技术
35次阅读
2025-03-20 09:51:10
如何使用 RisingWave、Kafka 和 Redis 构建实时推荐引擎
RisingWave中文开源社区
35次阅读
2025-03-10 10:30:31
Redis 集群主备切换原因分析
wzf0072
30次阅读
2025-03-20 17:51:42
Redis Cluster集群模式:构建大规模高性能分布式存储系统
老王两点中
27次阅读
2025-03-17 09:00:28
Redis 高可用方案
天翼云开发者社区
23次阅读
2025-03-24 17:09:54
拼多多二面:高并发场景扣减商品库存如何防止超卖?
码哥跳动
23次阅读
2025-03-11 08:36:28
高并发场景下的库存管理,理论与实战能否兼得?
京东云开发者
13次阅读
2025-03-24 16:54:56
如何使用 Redis完成 PV,UV 统计?
猿java
12次阅读
2025-03-04 08:02:34
Redis 集群使用(2)
巴韭特锁螺丝
9次阅读
2025-03-03 00:00:47