ssm开发使用redis作为缓存的使用步骤
程序员文章站
2024-03-05 21:34:37
1、关于spring配置文件中对于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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。