redis Jedis存取list对象和map
来源:互联网 发布:两个字符串相似度算法 编辑:程序博客网 时间:2024/05/16 09:35
1,redis缓存配置类
2,spring配置
3,序列化工具类
- public class RedisCache {
- protected static Logger logger = Logger.getLogger(RedisCache.class);
- public final static String VIRTUAL_COURSE_PREX = "_lc_vc_";
- private RedisCacheConfig redisCacheConfig;
- private JedisPool jedisPool = null;
- public RedisCacheConfig getRedisCacheConfig() {
- return redisCacheConfig;
- }
- public void setRedisCacheConfig(RedisCacheConfig redisCacheConfig) {
- this.redisCacheConfig = redisCacheConfig;
- }
- public RedisCache(){
- }
- /**
- * 初始化Redis连接池
- */
- private void initialPool(){
- JedisPoolConfig poolConfig = redisCacheConfig.getPoolConfig();
- String[] serverArray = redisCacheConfig.getServers().split(",");
- for(int i = 0; i < serverArray.length; i++){
- try {
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxTotal(poolConfig.getMaxTotal());
- config.setMaxIdle(poolConfig.getMaxIdle());
- config.setMaxWaitMillis(poolConfig.getMaxWaitMillis());
- config.setTestOnBorrow(poolConfig.getTestOnBorrow());
- config.setTestOnReturn(poolConfig.getTestOnReturn());
- jedisPool = new JedisPool(config, serverArray[i], redisCacheConfig.getPort(), redisCacheConfig.getTimeout());
- break;
- } catch (Exception e) {
- logger.error("initialPool create JedisPool(" + serverArray[i] + ") error : "+e);
- }
- }
- }
- /**
- * 在多线程环境同步初始化
- */
- private synchronized void poolInit() {
- if (jedisPool == null) {
- initialPool();
- }
- }
- /**
- * 同步获取Jedis实例
- * @return Jedis
- */
- public synchronized Jedis getJedis() {
- if (jedisPool == null) {
- poolInit();
- }
- Jedis jedis = null;
- try {
- if (jedisPool != null) {
- jedis = jedisPool.getResource();
- jedis.auth(redisCacheConfig.getAuth());
- }
- } catch (Exception e) {
- logger.error("Get jedis error : "+e);
- e.printStackTrace();
- }finally{
- returnResource(jedis);
- }
- return jedis;
- }
- /**
- * 释放jedis资源
- * @param jedis
- */
- public void returnResource(final Jedis jedis) {
- if (jedis != null && jedisPool !=null) {
- jedisPool.returnResource(jedis);
- }
- }
- /**
- * 得到Key
- * @param key
- * @return
- */
- public String buildKey(String key){
- return VIRTUAL_COURSE_PREX + key;
- }
- /**
- * 设置 String
- * @param key
- * @param value
- */
- public void setString(String key ,String value){
- try {
- value = StringUtil.isNullOrEmpty(value) ? "" : value;
- getJedis().set(buildKey(key),value);
- } catch (Exception e) {
- logger.error("Set key error : "+e);
- }
- }
- /**
- * 设置 过期时间
- * @param key
- * @param seconds 以秒为单位
- * @param value
- */
- public void setString(String key ,int seconds,String value){
- try {
- value = StringUtil.isNullOrEmpty(value) ? "" : value;
- getJedis().setex(buildKey(key), seconds, value);
- } catch (Exception e) {
- logger.error("Set keyex error : "+e);
- }
- }
- /**
- * 获取String值
- * @param key
- * @return value
- */
- public String getString(String key){
- String bKey = buildKey(key);
- if(getJedis() == null || !getJedis().exists(bKey)){
- return null;
- }
- return getJedis().get(bKey);
- }
- /**
- * 设置 list
- * @param <T>
- * @param key
- * @param value
- */
- public <T> void setList(String key ,List<T> list){
- try {
- getJedis().set(key.getBytes(),ObjectTranscoder.serialize(list));
- } catch (Exception e) {
- logger.error("Set key error : "+e);
- }
- }
- /**
- * 获取list
- * @param <T>
- * @param key
- * @return list
- */
- public <T> List<T> getList(String key){
- String bKey = buildKey(key);
- if(getJedis() == null || !getJedis().exists(key.getBytes())){
- return null;
- }
- byte[] in = getJedis().get(key.getBytes());
- List<T> list = (List<T>) ObjectTranscoder.deserialize(in);
- return list;
- }
- /**
- * 设置 map
- * @param <T>
- * @param key
- * @param value
- */
- public <T> void setMap(String key ,Map<String,T> map){
- try {
- getJedis().set(key.getBytes(),ObjectTranscoder.serialize(map));
- } catch (Exception e) {
- logger.error("Set key error : "+e);
- }
- }
- /**
- * 获取list
- * @param <T>
- * @param key
- * @return list
- */
- public <T> Map<String,T> getMap(String key){
- String bKey = buildKey(key);
- if(getJedis() == null || !getJedis().exists(key.getBytes())){
- return null;
- }
- byte[] in = getJedis().get(key.getBytes());
- Map<String,T> map = (Map<String, T>) ObjectTranscoder.deserialize(in);
- return map;
- }
- }
2,spring配置
- <!-- 声明redisCache -->
- <bean id="redisCache" class="com.cache.RedisCache" depends-on="redisCacheConfig">
- <property name="redisCacheConfig" ref="redisCacheConfig"></property>
- </bean>
- <bean id="redisCacheConfig" class="com.able.virtualcourse.cache.RedisCacheConfig" depends-on="jedisPoolConfig">
- <property name="servers"><value>${redis.servers}</value></property>
- <property name="port"><value>${redis.port}</value></property>
- <property name="auth"><value>${redis.auth}</value></property>
- <property name="timeout"><value>${redis.pool.timeout}</value></property>
- <property name="poolConfig" ref="jedisPoolConfig"></property>
- </bean>
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxTotal"><value>${redis.pool.maxTotal}</value></property>
- <property name="maxIdle"><value>${redis.pool.maxIdle}</value></property>
- <property name="maxWaitMillis"><value>${redis.pool.maxWait}</value></property>
- <property name="testOnBorrow"><value>${redis.pool.testOnBorrow}</value></property>
- <property name="testOnReturn"><value>${redis.pool.testOnReturn}</value></property>
- </bean>
3,序列化工具类
- public class ObjectTranscoder {
- public static byte[] serialize(Object value) {
- if (value == null) {
- throw new NullPointerException("Can't serialize null");
- }
- byte[] rv=null;
- ByteArrayOutputStream bos = null;
- ObjectOutputStream os = null;
- try {
- bos = new ByteArrayOutputStream();
- os = new ObjectOutputStream(bos);
- os.writeObject(value);
- os.close();
- bos.close();
- rv = bos.toByteArray();
- } catch (IOException e) {
- throw new IllegalArgumentException("Non-serializable object", e);
- } finally {
- try {
- if(os!=null)os.close();
- if(bos!=null)bos.close();
- }catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return rv;
- }
- public static Object deserialize(byte[] in) {
- Object rv=null;
- ByteArrayInputStream bis = null;
- ObjectInputStream is = null;
- try {
- if(in != null) {
- bis=new ByteArrayInputStream(in);
- is=new ObjectInputStream(bis);
- rv=is.readObject();
- is.close();
- bis.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- try {
- if(is!=null)is.close();
- if(bis!=null)bis.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return rv;
- }
- }
阅读全文
0 0
- redis Jedis存取list对象和map
- redis jedis存储对象简单操作,map list 自定义对象
- Redis存取List对象的实践
- Redis存取List对象的实践
- jedis 保存自定义List和Object对象
- redis缓存(jedis), 链接,存,取,String,map,对象
- redis操作string 、list 、map 对象
- Java map 存取对象
- Redis存取对象
- jedis调用redis之List
- list map Iterator 存取值
- Redis 和 Jedis
- Redis和jedis
- java 中的Map存取对象
- redis实现 spring-redis-data,存取对象
- 【redis,1】java操作redis: 将string、list、map、自定义的对象保存到redis中
- JAVA使用redis缓存的工具类,实现了javaBean,List集合,Map集合的直接存取
- Set和List的区别,List和Map的区别(介绍List,Set,Map对象结构
- linux awk命令详解
- java类加载器
- Java对象池原理分析
- 牛客网---2016---去哪儿寻找Coder
- GridView 鼠标移动变色
- redis Jedis存取list对象和map
- HTTP协议标准
- 【Swift】架构学习
- JavaWeb之JDBC
- jquery实现页面滑动到固定位置显示某个divhttp://blog.csdn.net/doudou_demo/article/details/47829249
- HTML_DOM事件
- 小白教程:win+linux+mac下如何安装Python环境集合
- MySQL存储引擎
- C++ list, vector, map, set 区别与用法比较