品优购电商系统开发第 8 章 五

4.5.5 Hash 类型操作
创建测试类 TestHash
1)存入值

@Test
public void testSetValue(){
redisTemplate.boundHashOps("namehash").put("a", "唐僧");
redisTemplate.boundHashOps("namehash").put("b", "悟空");
redisTemplate.boundHashOps("namehash").put("c", "八戒");
redisTemplate.boundHashOps("namehash").put("d", "沙僧");
}

2)提取所有的 KEY

@Test
public void testGetKeys(){
Set s = redisTemplate.boundHashOps("namehash").keys();
System.out.println(s);
}

运行结果:
[a, b, c, d]
3)提取所有的值

@Test
public void testGetValues(){
List values = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);
}

运行结果:
[唐僧悟空八戒沙僧]
4)根据 KEY 提取值

@Test
public void testGetValueByKey(){
Object object = redisTemplate.boundHashOps("namehash").get("b");
System.out.println(object);
}

运行结果:
悟空
5)根据 KEY 移除值

@Test
public void testRemoveValueByKey(){
redisTemplate.boundHashOps("namehash").delete("c");
}

运行后再次查看集合内容:
[唐僧悟空沙僧

5.网站首页-缓存广告数据
5.1 需求分析
现在我们首页的广告每次都是从数据库读取,这样当网站访问量达到高峰时段,对数据库压力很大,并且影响执行效率。我们需要将这部分广告数据缓存起来。
5.2 读取缓存
5.2.1 公共组件层
因为缓存对于我们整个的系统来说是通用功能。广告需要用,其它数据可能也会用到,所以我们将配置放在公共组件层(pinyougou-common)中较为合理。
1pinyougou-common 引入依赖

<!-- 缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>

2)创建配置文件
将资源中的redis-config.properties 和 applicationContext-redis.xml 拷贝至 pinyougou-common
3pinyougou-content-service 依赖 pinyougou-common
5.2.2 后端服务实现层
修改 pinyougou-content-service 的 ContentServiceImpl

@Autowired
private RedisTemplate redisTemplate;
@Override
public List<TbContent> findByCategoryId(Long categoryId) {
List<TbContent> contentList= (List<TbContent>)
redisTemplate.boundHashOps("content").get(categoryId);
if(contentList==null){
System.out.println("从数据库读取数据放入缓存");
//根据广告分类 ID 查询广告列表
TbContentExample contentExample=new TbContentExample();
Criteria criteria2 = contentExample.createCriteria();
criteria2.andCategoryIdEqualTo(categoryId);
criteria2.andStatusEqualTo("1");//开启状态
contentExample.setOrderByClause("sort_order");//排序
contentList = contentMapper.selectByExample(contentExample);//获取广告列
表
redisTemplate.boundHashOps("content").put(categoryId, contentList);//存
入缓存
}else{
System.out.println("从缓存读取数据");
}
return contentList;
}

5.3 更新缓存
当广告数据发生变更时,需要将缓存数据清除,这样再次查询才能获取最新的数据
5.3.1 新增广告后清除缓存
修改 pinyougou-content-service 工程 ContentServiceImpl.java 的 add 方法

/**
* 增加
*/
@Override
public void add(TbContent content) {
contentMapper.insert(content);
//清除缓存
redisTemplate.boundHashOps("content").delete(content.getCategoryId());
}

5.3.2 修改广告后清除缓存
考虑到用户可能会修改广告的分类,这样需要把原分类的缓存和新分类的缓存都清除掉。

/**
* 修改
*/
@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());
}
}

5.3.3 删除广告后清除缓存

/**
* 批量删除
*/
@Override
public void delete(Long[] ids) {
for(Long id:ids){
//清除缓存
Long categoryId = contentMapper.selectByPrimaryKey(id).getCategoryId();//广
告分类 ID
redisTemplate.boundHashOps("content").delete(categoryId);
contentMapper.deleteByPrimaryKey(id);
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值