向业务逻辑中添加缓存

一:导入jedis的jar包,或在maven项目中导入jedis依赖

二:书写配置文件

applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 配置单机版的连接 -->
	<bean id="jedisClientPool" class="lx.test.jedis.JedisClientPool"> 
		<property name="jedisPool" ref="jedisPool"></property> 
	</bean>
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> 
        <constructor-arg name="host" value="192.168.25.200"></constructor-arg> 
		<constructor-arg name="port" value="6379"></constructor-arg> 
	</bean>
	<!-- 配置集群版的连接 -->
<!-- 	<bean id="jedisClientCluster" class="lx.test.jedis.JedisClientCluster">
		<property name="jedisCluster" ref="jedisCluster"></property>
	</bean>
	<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="nodes">
			<set>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.200"></constructor-arg>
					<constructor-arg name="port" value="7001"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.200"></constructor-arg>
					<constructor-arg name="port" value="7002"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.200"></constructor-arg>
					<constructor-arg name="port" value="7003"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.200"></constructor-arg>
					<constructor-arg name="port" value="7004"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.200"></constructor-arg>
					<constructor-arg name="port" value="7005"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.200"></constructor-arg>
					<constructor-arg name="port" value="7006"></constructor-arg>
				</bean>
			</set>
		</constructor-arg>
	</bean> -->

</beans>

三:向业务逻辑中添加缓存

  1. 业务逻辑(查询为例)
  2. 查询缓存数据
  3. 查到直接返回
  4. 没有查到,设置查询条件,查询数据库
  5. 将查询结果添加到缓存 
  6. 返回查询列表 
@Override
		public List<TbContent> getContentListByCid(long cid) {
			//从缓存中查询数据
			try{
				String json = jedisClient.hget(CONTENT_LIST, cid+"");
				if(StringUtils.isNotBlank(json)){
					List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);
					return list;
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			//如果没有查询数据库
			TbContentExample example = new TbContentExample();
			Criteria criteria = example.createCriteria();
			//设置查询条件
			criteria.andCategoryIdEqualTo(cid);
			List<TbContent> list = tbContentMapper.selectByExampleWithBLOBs(example);
			//将查询结果添加至缓存
			try{
				jedisClient.hset(CONTENT_LIST, cid+"", JsonUtils.objectToJson(list));
			}catch(Exception e){
				e.printStackTrace();
			}
			return list;
		}

一旦业务逻辑添加了缓存,在做增删改操作时就需要同步缓存。(添加为例)

 

@Override
		public E3Result addContent(TbContent tbContent) {
			//将内容数据插入到内容表
			tbContent.setUpdated(new Date());
			tbContent.setCreated(new Date());
			//插入到数据库
			tbContentMapper.insert(tbContent);
			//缓存同步
			jedisClient.hdel(CONTENT_LIST, tbContent.getCategoryId().toString());
			return E3Result.ok();
		}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值