这里使用的是 Spring-4.3 , redis-2.8 的版本
 
1、添加maven依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
2、编写自己的redisAPI

package com.del.tools;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; /**
* 用于缓存的数据访问对象
* @author Domi
*/
public class RedisAPI { //引入jedis连接池对象
public JedisPool jedisPool;
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
} /**
* 缓存的设置
*/
public boolean set(String key,String value){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key,value);
return true;
} catch (Exception e) {
// TODO: handle exceptionr
e.printStackTrace();
}finally{
returnResource(jedisPool, jedis);
}
return false;
} /**
* 判断缓存中是否存在某个key值
*/
public boolean exist(String key){
Jedis jedis =null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key);
} catch (Exception e) {
// TODO: handle exception
}finally{
returnResource(jedisPool, jedis);
System.out.println("关闭成功===》");
}
return false;
} /**
* 获得缓存中的key
*/
public String get(String key){
String value = null;
Jedis jedis =null;
try {
jedis = jedisPool.getResource();
value = jedis.get(key);
} catch (Exception e) {
// TODO: handle exception
}finally{
returnResource(jedisPool, jedis);
}
return value;
} /**
* 返还jedisPool
*/
public static void returnResource(JedisPool jedisPool,Jedis jedis){
if (jedis!=null) {
jedisPool.returnResource(jedis);
}
}
}
 
3、用spring管理redis的bean组件

<!-- jedis连接池配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="1000"/> <!-- 控制一个pool可分配多少个jedis实例 -->
<property name="maxIdle" value="200" /> <!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->
<property name="maxWaitMillis" value="2000" /> <!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->
<property name="testOnBorrow" value="true" /> <!-- 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 -->
</bean> <!-- 连接jedis服务器 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
<constructor-arg ref="jedisPoolConfig"/>
<constructor-arg value="127.0.0.1"/>
<constructor-arg value="6379"/>
</bean> <!-- 配置redisAPI -->
<bean id="redisAPI" class="com.del.tools.RedisAPI">
<property name="jedisPool" ref="jedisPool"/>
</bean>
 
4、在业务中使用redisAPI
/*
* 根据userID查询购物车列表
* 使用redis缓存
*/
@RequestMapping(value="/list")
public ModelAndView doBuyCarList(HttpSession session){ ModelAndView madnv = new ModelAndView();
User user = (User)session.getAttribute("hasLogin");
if(user!=null){ //先判斷redis裡面有沒有值
List<BuyCar> buycarlist1 = null;
buycarlist1 = buyCarService.findBuyCarList(user.getUserid());
String buycarlist = JSON.toJSONString(buycarlist1,true);
redisAPI.set("carlist"+user.getUserid(), buycarlist);
madnv.addObject("buycarlist", buycarlist1);
System.out.println("buycarlist====="+buycarlist1);
System.out.println("FROM DB==>"); }else{
String buycarlist = redisAPI.get("carlist"+user.getUserid());
if (buycarlist!=null && !"".equals(buycarlist)) {
buycarlist1 =JSON.parseArray(buycarlist, BuyCar.class);
madnv.addObject("buycarlist", buycarlist1);
System.out.println("buycarlist====="+buycarlist);
System.out.println("FROM REDIS==>");
}else{
System.out.println("error==>");
}
}
}
madnv.setViewName("buycar/shopping.jsp");
return madnv;
}

redis—Spring中redis缓存的简单使用的更多相关文章

  1. 分布式数据存储 之 Redis(二) —— spring中的缓存抽象

    分布式数据存储 之 Redis(二) -- spring中的缓存抽象 一.spring boot 中的 StringRedisTemplate 1.StringRedisTemplate Demo 第 ...

  2. java项目中ehcache缓存最简单用法

      java项目中ehcache缓存最简单用法: 1.下载ehcache-core-2.4.3.jar复制到项目的lib目录下 2.新建ehcache.xml文件,放置在项目src目录下的resour ...

  3. 浅析redis缓存 在spring中的配置 及其简单的使用

    一:如果你需要在你的本地项目中配置redis.那么你首先得需要在你的本地安装redis 参考链接[http://www.runoob.com/redis/redis-install.html] 下载r ...

  4. spring中使用缓存

    一.启用对缓存的支持 Spring 对缓存的支持最简单的方式就是在方法上添加@Cacheable和@CacheEvict注解, 再添加注解之前,必须先启用spring对注解驱动的支持,基于java的配 ...

  5. spring中配置缓存—ehcache

    常用的缓存工具有ehcache.memcache和redis,这里介绍spring中ehcache的配置. 1.在pom添加依赖: <!-- ehcache 相关依赖 --> <de ...

  6. Spring中ClassPathXmlApplicationContext类的简单使用

    转自:http://www.cnblogs.com/shyy/archive/2011/09/29/2453057.html 一.简单的用ApplicationContext做测试的话,获得Sprin ...

  7. spring中的缓存--Caching

    1.spring从3.1开始支持缓存功能.spring 自带的缓存机制它只在方法上起作用,对于你使用其他持久化层的框架来讲,是没有影响的,相对来讲这种缓存方式还是不错的选择. 2.提供缓存的接口:or ...

  8. 转:spring中InitailizingBean接口的简单理解

    转自:https://www.cnblogs.com/wxgblogs/p/6849782.html spring中InitializingBean接口使用理解   InitializingBean接 ...

  9. spring中redistemplate不能用通配符keys查出相应Key的问题

    有个业务中需要删除某个前缀的所有Redis缓存,于是用RedisTemplate的keys方法先查出所有合适的key,再遍历删除.但是在keys(patten+"*")时每次取出的 ...

随机推荐

  1. python2.0 s12 day4

    python2.0 s12 day404 python s12 day4 TengLan回顾上节内容 05 python s12 day4 迭代器原理及使用 本节大纲介绍: 1.迭代器&生成器 ...

  2. storm中的基本概念

    Storm是一个流计算框架,处理的数据是实时消息队列中的,所以需要我们写好一个topology逻辑放在那,接收进来的数据来处理,所以是通过移动数据平均分配到机器资源来获得高效率. Storm的优点是全 ...

  3. img标签-srcset属性

    今天看前辈的代码时,发现img标签有个陌生的srcset属性,如下: 1 <img class="Avatar" src="https://pic3.zhimg.c ...

  4. .Net内存溢出 System.OutOfMemoryException

    内存溢出常见的情况和处理方式: http://outofmemory.cn/c/dotNet-outOfMemoryException MSDN中关于processModel的文档 https://m ...

  5. activemq 实战三 了解连接器的URI-Understanding connector URIs

    Before discussing the details of connectors and their role in the overall ActiveMQ architecture, it’ ...

  6. 【Spring Boot && Spring Cloud系列】构建Springboot项目 实现restful风格接口

    项目代码如下: package hello; import org.springframework.boot.SpringApplication; import org.springframework ...

  7. MongoDb Mmap引擎分析

    版权声明:本文由孔德雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/137 来源:腾云阁 https://www.qclo ...

  8. JavaWeb温习之HttpServletResponse对象

    以下内容均根据"方立勋JavaWeb视频教程"进行总结 1. HttpServletResponse常见应用——设置响应头控制浏览器的行为 1.1 设置http响应头控制浏览器禁止 ...

  9. SenchaTouch调用纯数字键盘

    items:[ { itemId:"phoneNumber", xtype: "textfield", component:{xtype:"input ...

  10. click() bind() live() delegate()区别

    click(),bind(),live()都是执行事件时使用的方法 1.click()单击事件方法: $("a").click(function() { alert("h ...