Redis工作笔记-spring整合jedis
生活随笔
收集整理的這篇文章主要介紹了
Redis工作笔记-spring整合jedis
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
概念
代碼與實例
?
概念
這里,一般都是用jedis去搞項目,而不是用spring自帶的。
主要是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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- Spring聚合配置 --><context:property-placeholder location="redis.properties" /><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="${redis.maxTotal}" /><property name="maxIdle" value="${redis.maxIdle}" /><property name="minIdle" value="${redis.minIdle}" /></bean><bean id="jedisPool" class="redis.clients.jedis.JedisPool"><constructor-arg name="poolConfig" ref="poolConfig" /><constructor-arg name="host" value="${redis.host}" /><constructor-arg name="port" value="${redis.port}" /><constructor-arg name="timeout" value="${redis.timeout}" /><constructor-arg name="password" value="${redis.password}" /></bean><bean class="redisutil.demo.utils.SpringBeanHolder" /></beans>Bean加載如下:
package redisutil.demo.utils;import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;public class SpringBeanHolder implements ApplicationContextAware {private static ApplicationContext ac;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ac = applicationContext;}public static Object getBean(String beanName){return ac.getBean(beanName);}public static<T> T getBean(Class<T> clazz){return ac.getBean(clazz);} }?
代碼與實例
程序運行截圖如下:
Redis運行截圖如下:
添加了數據后:
另外的數據:
工具類如下:
package redisutil.demo.utils;import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;public class RedisUtils {private static JedisPool jedisPool = null;static {jedisPool = (JedisPool)SpringBeanHolder.getBean("jedisPool");}public static void set(String key, String value){Jedis jedis = null;try {if(jedis == null){jedis = jedisPool.getResource();jedis.set(key, value);}}catch (Exception e){e.printStackTrace();}finally {jedis.close();}}public static void set(String key, String value, int expireTime){Jedis jedis = null;try {if(jedis == null){jedis = jedisPool.getResource();jedis.set(key, value);jedis.expire(key, expireTime);}}catch (Exception e){e.printStackTrace();}finally {jedis.close();}}public static String get(String key, String value){return null;}}源碼打包下載地址:
https://github.com/fengfanchen/Java/tree/master/RedisUtil
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Redis工作笔记-spring整合jedis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QML笔记-2种方式实现心脏跳动
- 下一篇: Qt笔记-Q3DScatter及QCus