Redis缓存实现广告页面迅速读取(SpringDataRedis 框架)

应用场景:

首页每天有大量的人访问,对数据库造成很大的访问压力,甚至是瘫痪。
那如何解决呢?我们通常的做法有两种:一种是数据缓存、一种是网页静态化。

***使用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);
		
		}

	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值