ssm开发使用redis作为缓存的使用步骤
更新时间:2018年11月01日 14:54:38 作者:羽哲
在开发中经常遇到大量的重复的,高并发的查询,此时可以使用redis缓存。这篇文章主要介绍了ssm开发使用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 * @author luzhewu * */ 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); } } }
3、redis相关依赖
<!-- redis相关依赖jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.1</version>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
IDEA连接MySQL提示serverTimezone的问题及解决方法
很多朋友私聊小编,使用IDEA软件连接MySQL数据库时总是提示Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.的错误,小编就不一一回复大家了,下面小编把我的解决方法分享到脚本之家平台,需要的朋友参考下吧2021-05-05Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
这篇文章主要介绍了Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下2018-01-01IntelliJ IDEA修改新建文件自动生成注释的user名
今天小编就为大家分享一篇关于IntelliJ IDEA修改新建文件自动生成注释的user名,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2018-10-10听说用了YYYY-MM-dd的程序员,前些天都在加班改Bug
这篇文章主要介绍了YYYY-MM-dd的实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-01-01
最新评论