码迷,mamicode.com
首页 > 其他好文 > 详细

自定义对象存入Redis

时间:2018-07-20 13:59:49      阅读:553      评论:0      收藏:0      [点我收藏+]

标签:ast   bytes   client   ati   factory   get   cond   ali   type   

package com.qiyi.tvguo.cms.common;

import com.alibaba.fastjson.JSON;
import com.qiyi.tvguo.cms.common.utils.ObjectSerializeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * redis client
 */
@Component
@Slf4j
public class RedisClient {

    private static  JedisPool pool;

    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private Integer port;

    @Value("${redis.password}")
    private String password;

    private  final int timeout=5000;
    private  final int database=0;
    private  final int maxConnection=500;
    private  final int maxIdle=50;
    private  final int minIdle=20;

    @PostConstruct
    public   void  init()
    {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxConnection);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxWaitMillis(1000);
        config.setTestOnBorrow(true);
        pool = new JedisPool(config, host,port,timeout, password, database);
        log.info("------jedis pool init------");
    }

    /**
     * 获取jedis
     * @return
     */
    public Jedis getJedis()
    {
        return pool.getResource();
    }


    @PreDestroy
    public  void  destroy()
    {
        if(pool==null)
        {
            return;
        }

        pool.destroy();
        log.info("-----jedis pool destroy-----");
    }


    /**
     * 复杂对象存入redis
     * @param key
     * @param v
     * @param expireSeconds
     * @param <T>
     * @return
     */
    public <T> Boolean setExByte(String key,T v,int expireSeconds)
    {
        Jedis jedis=null;
        try
        {
            jedis= getJedis();
            jedis.setex(key.getBytes(),expireSeconds,ObjectSerializeUtil.serialize(v));
            return true;

        }catch (Exception ex)
        {
            log.error("复杂对象存入redis异常:{}",ex);

        }finally {
            if(jedis!=null)
            {
                jedis.close();
            }
        }

        return  false;
    }


    /**
     * 获取Value 为byte[]的复杂对象
     * @param key
     * @param <T>
     * @return
     */
    public <T> T getByteObject(String key)
    {
        Jedis jedis=null;
        try
        {
            jedis= getJedis();
            byte[] bytes= jedis.get(key.getBytes());
            return  (T)ObjectSerializeUtil.unserizlize(bytes);

        }catch (Exception ex)
        {
            log.error("redis获取复杂对象异常:{}",ex);

        }finally {
            if(jedis!=null)
            {
                jedis.close();
            }
        }

        return  null;
    }


    /**
     * 对象以JSON存入redis
     * @param key
     * @param obj
     * @param expireSeconds
     * @param <T>
     * @return
     */
    public <T> Boolean setExJson(String key,T obj,int expireSeconds)
    {
        Jedis jedis=null;
        try
        {
            jedis= getJedis();
            jedis.setex(key,expireSeconds, JSON.toJSONString(obj));
            return true;

        }catch (Exception ex)
        {
            log.error("复杂对象存入redis异常:{}",ex);
        }finally {
            if(jedis!=null)
            {
                jedis.close();
            }
        }

        return  false;
    }


    /**
     * 获取Value 为Json的复杂对象
     * @param key
     * @param <T>
     * @return
     */
    public <T extends  Object> T getJsonObject(String key,Class<T> clazz)
    {
        Jedis jedis=null;
        try
        {
            jedis= getJedis();
            String jsonString= jedis.get(key);
          return JSON.parseObject(jsonString, clazz);

        }catch (RuntimeException ex)
        {
            log.error("redis获取Json复杂对象异常:{}",ex);

        } finally {
            if(jedis!=null)
            {
                jedis.close();
            }
        }

        return null;
    }



}

 

自定义对象存入Redis

标签:ast   bytes   client   ati   factory   get   cond   ali   type   

原文地址:https://www.cnblogs.com/red-fox/p/9340911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!