posir 发表于 2018-11-5 12:35:50

spring整合redis缓存

package com.layou;  

  
import java.io.ByteArrayInputStream;
  
import java.io.ByteArrayOutputStream;
  
import java.io.IOException;
  
import java.io.ObjectInputStream;
  
import java.io.ObjectOutputStream;
  

  
import org.springframework.cache.Cache;
  
import org.springframework.cache.support.SimpleValueWrapper;
  
import org.springframework.dao.DataAccessException;
  
import org.springframework.data.redis.connection.RedisConnection;
  
import org.springframework.data.redis.core.RedisCallback;
  
import org.springframework.data.redis.core.RedisTemplate;
  

  
public class RedisCache implements Cache {
  

  
    private RedisTemplate redisTemplate;
  
    private String name;
  

  
    public RedisTemplate getRedisTemplate() {
  
      return redisTemplate;
  
    }
  

  
    public void setRedisTemplate(RedisTemplate redisTemplate) {
  
      this.redisTemplate = redisTemplate;
  
    }
  

  
    public void setName(String name) {
  
      this.name = name;
  
    }
  

  
    @Override
  
    public String getName() {
  
      return this.name;
  
    }
  

  
    @Override
  
    public Object getNativeCache() {
  
      return this.redisTemplate;
  
    }
  

  
    @Override
  
    public ValueWrapper get(Object key) {
  
      final String keyf = (String) key;
  
      Object object = null;
  
      object = redisTemplate.execute(new RedisCallback() {
  
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
  

  
                byte[] key = keyf.getBytes();
  
                byte[] value = connection.get(key);
  
                if (value == null) {
  
                  return null;
  
                }
  
                return toObject(value);
  

  
            }
  
      });
  
      return (object != null ? new SimpleValueWrapper(object) : null);
  
    }
  

  
    @Override
  
    public void put(Object key, Object value) {
  
      final String keyf = (String) key;
  
      final Object valuef = value;
  
      final long liveTime = 86400;
  

  
      redisTemplate.execute(new RedisCallback() {
  
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
  
                byte[] keyb = keyf.getBytes();
  
                byte[] valueb = toByteArray(valuef);
  
                connection.set(keyb, valueb);
  
                if (liveTime > 0) {
  
                  connection.expire(keyb, liveTime);
  
                }
  
                return 1L;
  
            }
  
      });
  
    }
  

  
    @Override
  
    public void evict(Object key) {
  
      final String keyf = (String) key;
  
      redisTemplate.execute(new RedisCallback() {
  
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
  
                return connection.del(keyf.getBytes());
  
            }
  
      });
  
    }
  

  
    @Override
  
    public void clear() {
  
      redisTemplate.execute(new RedisCallback() {
  
            public String doInRedis(RedisConnection connection) throws DataAccessException {
  
                connection.flushDb();
  
                return "ok";
  
            }
  
      });
  
    }
  

  
    /**
  
   * 描述 : .
  
   *
  
   *
  
   *
  
   *
  
   * @param obj
  
   * @return
  
   */
  
    private byte[] toByteArray(Object obj) {
  
      byte[] bytes = null;
  
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
  
      try {
  
            ObjectOutputStream oos = new ObjectOutputStream(bos);
  
            oos.writeObject(obj);
  
            oos.flush();
  
            bytes = bos.toByteArray();
  
            oos.close();
  
            bos.close();
  
      } catch (IOException ex) {
  
            ex.printStackTrace();
  
      }
  
      return bytes;
  
    }
  

  
    /**
  
   * 描述 : .
  
   *
  
   *
  
   *
  
   *
  
   * @param bytes
  
   * @return
  
   */
  
    private Object toObject(byte[] bytes) {
  
      Object obj = null;
  
      try {
  
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  
            ObjectInputStream ois = new ObjectInputStream(bis);
  
            obj = ois.readObject();
  
            ois.close();
  
            bis.close();
  
      } catch (IOException ex) {
  
            ex.printStackTrace();
  
      } catch (ClassNotFoundException ex) {
  
            ex.printStackTrace();
  
      }
  
      return obj;
  
    }
  
}


页: [1]
查看完整版本: spring整合redis缓存