ssm开发使用redis作为缓存,使用步骤
2017-12-06 19:58
441 查看
转自:https://www.cnblogs.com/wiseroll/p/7258863.html
1、关于spring配置文件中对于redis的配置
2、配置自定义的RedisAPI,对redis数据库的管理
1、关于spring配置文件中对于redis的配置
<!-- redis配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- <property name="maxActive" value="90"/> --> <property name="maxIdle" value="5"/> <!-- <property name="maxWait" value="1000"/> --> <property name="testOnBorrow" value="true"/> </bean> <!--配置redis数据源--> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy"> <constructor-arg ref="jedisPoolConfig"/> <constructor-arg value="192.168.21.195"/> <constructor-arg value="6379"/> </bean> <!--配置自定义的RedisAPI工具类--> <bean id="redisAPI" class="org.slsale.common.RedisAPI"> <property name="jedisPool" ref="jedisPool"/> </bean>
2、配置自定义的RedisAPI,对redis数据库的管理
package org.slsale.common; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * jedisAPI */ public class RedisAPI { public JedisPool jedisPool;// redis连接池对象 public JedisPool getJedisPool() { return jedisPool; } public void setJedisPool(JedisPool jedisPool) { this.jedisPool = jedisPool; } /** * set key and value tp redis * @param key * @param value * @return */ public boolean set(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource();// 获取jedis对象 jedis.set(key, value); return true; } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 returnResource(jedisPool, jedis); } return false; } /** * 判断某个key是否存在 * @param key * @return */ public boolean exist(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.exists(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 returnResource(jedisPool, jedis); } return false; } /** * 通过key获取value * @param key * @return */ public String get(String key) { String value = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 returnResource(jedisPool, jedis); } return value; } /** * 返还到连接池 * @param jedisPool * @param jedis */ public static void returnResource(JedisPool jedisPool, Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } } }
相关文章推荐
- ssm开发使用redis作为缓存,使用步骤
- SpringBank 开发日志 Mybatis 使用redis 作为二级缓存时,无法通过cacheEnabled=false 将其关闭
- (spring-data-redis)SSM框架下使用redis作为mybatis的二级缓存
- REDIS学习(3.2)spring boot 使用redis作为缓存
- Magento 2 页面缓存和默认缓存中使用Redis - Magento2开发教程
- Java项目开发心得(二):使用EhCache+SSM实现数据缓存
- mybatis整合redies,使用redis作为二级缓存
- 【新手总结】在.Net项目中使用Redis作为缓存服务
- centos6.6安装redis服务安装redis服务,对于discuz来说可以作为缓存使用,减轻服务器压力
- SSM框架——Spring+SpringMVC+Mybatis的搭建教程 一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛。 Spring作为一个轻量级
- 在项目开发中使用Redis作缓存
- 使用走_Unix_Sockets_的_Redis_作为对象缓存
- iReport 使用JavaBean作为数据源开发报表(SSM)
- Redis 3.0官方文档翻译计划(8) ——使用Redis作为LRU缓存
- 使用redis作为缓存,数据还需要存入数据库中吗?
- 使用redis作为缓存
- nodejs使用redis作为缓存介质,封装缓存类
- Golang web 开发实战之 session 缓存:如何使用 redigo 将一个结构体数据保存到 redis?
- Golang web 开发实战之 session 缓存:如何使用 redigo 将一个结构体数据保存到 redis?
- 使用Redis作为LRU缓存