spring_data_redis 的应用一

将mysql数据库经常访问的主页数据放入spring_data_redis中

顺便说一下怎么启动redis,本人将redis已经安装在了虚拟机中,并在一台linux机器上安装了redis,所以,启动如下

第一步:首先切换到redis下的bin目录,    我这边redis的bin目录路径为 cd /export/servers/redis/bin/

第二步:启动该目录下的redis-server 和其配置文件目录下的redis_6379.conf配置文件

命令为:  ./redis-server ../conf/redis-6379.conf

 

第一步:导入jar包

导入pyg_common项目

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

第二步:创建配置文件

redis-config-properties的内容如下

# Redis settings 
# server IP
#装有redis数据库的主机ip地址
redis.host=192.168.5.111    
# server port
#redis默认的端口号
redis.port=6379
# server pass
#密码:这里不写,默认为空,自动登录
redis.pass=
# use dbIndex 
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
#redis最大空闲连接数量300个
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
#redis的最长等待时间3秒
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684  
redis.testOnBorrow=true

 

applicationContext-redis.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:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
   <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>  
      
</beans>  

 

第三步 :哪个项目需要redis就将conmon的jar包引到自己的pom.xmlz中

此处,我们是pyg_content_service需要redis服务,因此引入

<dependency>
    <groupId>com.pyg</groupId>
    <artifactId>pyg_common</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

第四步:在pyg_content_service项目下就可以使用了

获取redisTemplate对象就可以操作

package com.pyg.content.service.impl;
import java.util.List;
import com.alibaba.dubbo.config.annotation.Service;
import com.pyg.mapper.TbContentMapper;
import com.pyg.pojo.TbContent;
import com.pyg.pojo.TbContentExample;
import com.pyg.pojo.TbContentExample.Criteria;
import com.pyg.content.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class ContentServiceImpl implements ContentService {

   @Autowired
   private TbContentMapper contentMapper;
   @Autowired
   private RedisTemplate redisTemplate;

   //新增方法,根据不同的categoryid 传送不同的content广告信息
   @Override
   public List<TbContent> findContentByCategoryId(Long categoryId) {
      List<TbContent> tbContents=(List<TbContent>)redisTemplate.boundHashOps("content").get(categoryId);
      if(tbContents==null){
         TbContentExample example = new TbContentExample();
         Criteria criteria = example.createCriteria();
         criteria.andCategoryIdEqualTo(categoryId);
         criteria.andStatusEqualTo("1");//开启状态
         //重点:排序
         example.setOrderByClause("sort_order");
         tbContents = contentMapper.selectByExample(example);//获取广告列表
         redisTemplate.boundHashOps("content").put(categoryId,tbContents);
         System.out.println("从mysql数据库读取");
      }else {
         System.out.println("从redis缓存读取");
      }

      return tbContents;
   }

}

 

第五步:  测试看结果

第一次是从mysql数据库获取,之后每一次都从redis缓存读取

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值