在实际使用缓存的过程中,我们希望放入缓存的并不是一个String类型参数而是一个Object对象,而且希望从缓存中取出的也是一个Object对象
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <aop:aspectj-autoproxy /> <context:annotation-config /> <context:component-scan base-package="com.cathy.demo.redis.*" /> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="127.0.0.1" /> <constructor-arg name="port" value="6379" /> <constructor-arg name="timeout" value="2000" /> </bean> </list> </constructor-arg> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="50" /> <property name="maxIdle" value="8" /> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> </bean> <bean id="redisClient" class="com.cathy.demo.redis.RedisClientImpl"/> <bean id="redisExecuteTemplate" class="com.cathy.demo.redis.RedisExecuteTemplate"/> </beans>
** * 序列化工具 * @author zhangwei_david * @version $Id: SerializationUtil.java, v 0.1 2014年12月31日 下午5:41:35 zhangwei_david Exp $ */ public class SerializationUtil { private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>(); private static Objenesis objenesis = new ObjenesisStd(true); private static <T> Schema<T> getSchema(Class<T> clazz) { @SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) cachedSchema.get(clazz); if (schema == null) { schema = RuntimeSchema.getSchema(clazz); if (schema != null) { cachedSchema.put(clazz, schema); } } return schema; } /** * 序列化 * * @param obj * @return */ public static <T> byte[] serializer(T obj) { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema<T> schema = getSchema(clazz); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } /** * 反序列化 * * @param data * @param clazz * @return */ public static <T> T deserializer(byte[] data, Class<T> clazz) { try { T obj = objenesis.newInstance(clazz); Schema<T> schema = getSchema(clazz); ProtostuffIOUtil.mergeFrom(data, obj, schema); return obj; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } } }
/** * * @author zhangwei_david * @version $Id: RedisTemplate.java, v 0.1 2015年6月6日 下午6:21:26 zhangwei_david Exp $ */ @Component public class RedisExecuteTemplate { @Autowired private ShardedJedisPool shardedJedisPool; public ShardedJedis getRedisClient() { try { ShardedJedis shardJedis = shardedJedisPool.getResource(); return shardJedis; } catch (Exception e) { } return null; } @SuppressWarnings("deprecation") public void returnResource(ShardedJedis shardedJedis) { shardedJedisPool.returnResource(shardedJedis); } public Object excute(ExecuteCallback executeCallback) { ShardedJedis shardedJedis = getRedisClient(); if (shardedJedis == null) { return null; } try { // 通过回调方法执行具体执行 return executeCallback.command(shardedJedis); } catch (Exception e) { } finally { // 释放资源 returnResource(shardedJedis); } return null; } /** * * * @author zhangwei_david * @version $Id: RedisExecuteTemplate.java, v 0.1 2015年6月6日 下午7:45:58 zhangwei_david Exp $ */ public interface ExecuteCallback { public Object command(ShardedJedis shardedJedis); } }
/** * * @author zhangwei_david * @version $Id: RedisClient.java, v 0.1 2015年6月6日 下午7:38:43 zhangwei_david Exp $ */ public interface RedisClient { /** * * * @param key * @param obj * @param expire * @return */ public boolean putObjectWithExpire(String key, Object obj, long expire); /** * * 从Redis缓存中获取key指定的对象 * @param key * @param clazz * @return */ public Object getObjectByKey(String key, Class<?> clazz); }
/** * * @author zhangwei_david * @version $Id: RedisClient.java, v 0.1 2015年6月6日 下午6:35:25 zhangwei_david Exp $ */ @Component("redisClient") public class RedisClientImpl implements RedisClient { @Autowired private RedisExecuteTemplate redisExecuteTemplate; /** * * @see com.cathy.demo.redis.RedisClient#putObjectWithExpire(java.lang.String, java.lang.Object, long) */ public boolean putObjectWithExpire(final String key, final Object obj, final long expireTime) { String result = (String) redisExecuteTemplate.excute(new ExecuteCallback() { byte[] objSeria = SerializationUtil.serializer(obj); public Object command(ShardedJedis shardedJedis) { return shardedJedis.set(key, new String(objSeria), "nx", "ex", expireTime); } }); return "OK".equals(result); } /** * * @see com.cathy.demo.redis.RedisClient#getObjectByKey(java.lang.String, java.lang.Class) */ public Object getObjectByKey(final String key, final Class<?> clazz) { return redisExecuteTemplate.excute(new ExecuteCallback() { public Object command(ShardedJedis shardedJedis) { String str = shardedJedis.get(key); return SerializationUtil.deserializer(str.getBytes(), clazz); } }); } }
/** * * @author zhangwei_david * @version $Id: RedisClientTest.java, v 0.1 2015年6月6日 下午6:38:24 zhangwei_david Exp $ */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:META-INF/spring/jedis-beans.xml") public class RedisClientTest { @Autowired private RedisClientImpl redisClient; @Test public void testSet() { redisClient.putObjectWithExpire("my1.person.test", new Person(), 100); Person person = (Person) redisClient.getObjectByKey("my1.person.test", Person.class); System.out.println(person.getName()); } }
log4j:WARN custom level class [# 输出DEBUG级别以上的日志] not found. 2015-06-06 19:43:09 [ main:0 ] - [ INFO ] @TestExecutionListeners is not present for class [class com.cathy.demo.redis.RedisClientTest]: using defaults. 2015-06-06 19:43:09 [ main:145 ] - [ INFO ] Loading XML bean definitions from URL [file:/H:/Alipay.com/workspace4alipay/demo/target/classes/META-INF/spring/jedis-beans.xml] 2015-06-06 19:43:09 [ main:314 ] - [ INFO ] JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning 2015-06-06 19:43:09 [ main:333 ] - [ INFO ] Refreshing org.springframework.context.support.GenericApplicationContext@43840: startup date [Sat Jun 06 19:43:09 CST 2015]; root of context hierarchy 2015-06-06 19:43:09 [ main:489 ] - [ INFO ] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@525845: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,shardedJedisPool,jedisPoolConfig,redisClient,redisExecuteTemplate,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy Test 2015-06-06 19:43:09 [ Thread-3:742 ] - [ INFO ] Closing org.springframework.context.support.GenericApplicationContext@43840: startup date [Sat Jun 06 19:43:09 CST 2015]; root of context hierarchy 2015-06-06 19:43:09 [ Thread-3:743 ] - [ INFO ] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@525845: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,shardedJedisPool,jedisPoolConfig,redisClient,redisExecuteTemplate,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
相关推荐
spring-data集成jedis测试代码,spring配置文件及jar包。
Redis Windows32位+64位安装包,Linux压缩包, Redis图形操作管理软件,Jedis
Shiro + JWT + SpringBoot + MySQL + Redis(Jedis)实现无状态鉴权机制(Restful风格API)(改为数据库形式(MySQL)) 博客源码 附件
前后端分离(这里只展示后台),基于url拦截的通用权限管理系统,采用jwt+redis的机制取代传统session+cookie的认证授权方式,shiro框架,配置Jedis,以redis作缓存
redis2.8windows64位版本+jedis2.5jar+jedis英文api
redis3.2.1+jedis2.8.0+jedis.api.rar一站下载
在spring+spring mvc+mybatis模式下,使用的最多的就是jedis,但是spring boot整合了redis后,依然可以使用jedis,但是同时也提供了一个RedisTemplate和StringRedisTemplate,RedisTemplate使用的序列化类是默认...
里面包含了spring-data-redis-1.3.4.RELEASE.jar ,但是jedis可以不用,用spring-data-redis方式的同学可以使用
redis3.2.1+jedis2.5+jedis.api一站下载
spring boot集成redis,使用redis官网推荐java语言jedis操作
spring-session+spring++redis的依赖包,jedis-2.7.3,spring-data-redi,spring-session-1+commons-pool2
redis辅助文档 + jedis/
spring-data-redis-1.6.0.RELEASE.jar + jedis-2.7.2.jar,另:相关的配置文件在我的代码片段中
spring分片+jedis+sentinel
Redis+Jedis+案例实战(高清视频教程).rar
redis3.0 java下客户端jedis
在Springboot中已经有实现好的jar包可以很方便的集成Redis,也可以自己来封装Jedis实现Redis,这里我们使用Jedis来封装,从而使我们的程序更具有灵活性 首先,我们需要安装Redis: redis下载 安装完成之后在控制台输入...
本文阐述了jedis与redis相对应的各类方法,方便开发者根据需求调用
只要你的redis已经准备好即可。该项目需要maven依赖,请自行安装maven。然后把redis的链接地址改一下,用tomcat发布项目之后,进入项目:浏览器地址栏输入...
spring-data-redis-2.1.5.RELEASE和jedis2.9.0,用于java使用redis开发。