标签:缓存 redis Redis contentMapper content 广告页面 SpringDataRedis categoryId redisTempla
应用场景:
首页每天有大量的人访问,对数据库造成很大的访问压力,甚至是瘫痪。
那如何解决呢?我们通常的做法有两种:一种是数据缓存、一种是网页静态化。
***使用Redis的之前考虑的四个问题? ***
1.在查询数据库之前,先查询是否有缓存数据,
2.查询数据库之后,吧查询出来的数据添加到缓存中.
3.缓存只是一种优化不能影响正常业务进行.
4.有缓存就要考虑缓存同步问题,是否和数据库同步
1.Redis依赖导入
<!-- 缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
2.配置redis-config.properties 文件
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
3.配置创建 applicationContext-redis.xml 文件
maxIdle :最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个 jedis 实例时,是否提前进行验证操作;如果为 true,则得到的 jedis 实例均是可用的;
<context:property-placeholder location="classpath*:properties/*.properties" />
<!-- 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>
**
4.服务层进行缓存判断.
**
@Autowired
private TbContentMapper contentMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public List<TbContent> findByCategoryId(Long categoryId) {
//根据分类ID查询
List<TbContent> list = (List<TbContent>) redisTemplate.boundHashOps("content").get(categoryId);
//如果缓存中有则直接读取缓存中的数据
if (list != null) {
System.out.println("查询缓存");
return list;
}
try {
TbContentExample example = new TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(categoryId); // 符合该ID的
criteria.andStatusEqualTo("1");// 状态为1(有效)
example.setOrderByClause("sort_order"); // 根据序号进行排序
list = contentMapper.selectByExample(example);
redisTemplate.boundHashOps("content").put(categoryId, list);
System.out.println("查询数据库");
} catch (Exception e) {
System.out.println("查询异常");
e.printStackTrace();
}
return list;
}
5.更新缓存
增删改后都应该对缓存进行清除,实现立即同步.
/**
* 增加
*/
@Override
public void add(TbContent content) {
contentMapper.insert(content);
redisTemplate.boundHashOps("content").delete(content.getCategoryId());
}
/**
* 修改
*/
@Override
public void update(TbContent content) {
// 查询原来的分组ID
Long categoryId = contentMapper.selectByPrimaryKey(content.getId()).getCategoryId();
redisTemplate.boundHashOps("content").delete(categoryId );
contentMapper.updateByPrimaryKey(content);
//如果修改之后的ID和修改前的ID不同,则修改后再清除一次
if(categoryId.longValue()!=content.getCategoryId().longValue()) {
redisTemplate.boundHashOps("content").delete(content.getCategoryId());
}
}
/**
* 批量删除
*/
@Override
public void delete(Long[] ids) {
for (Long id : ids) {
//删除之前清除缓存
redisTemplate.boundHashOps("content").delete(contentMapper.selectByPrimaryKey(id).getCategoryId());
contentMapper.deleteByPrimaryKey(id);
}
}
标签:缓存,redis,Redis,contentMapper,content,广告页面,SpringDataRedis,categoryId,redisTempla 来源: https://blog.csdn.net/weixin_43342054/article/details/98348181
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。