All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cudoy.framework.cache.redis.RedisCacheService Maven / Gradle / Ivy

The newest version!
package com.cudoy.framework.cache.redis;

import com.cudoy.framework.cache.AbstractCacheService;
import io.lettuce.core.RedisFuture;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.lettuce.LettuceConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import redis.clients.jedis.DebugParams;
import redis.clients.jedis.Jedis;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class RedisCacheService extends AbstractCacheService{

    private RedisTemplate redisTemplate;
    private String namespace;

    public RedisCacheService(String namespace){
        this.namespace = namespace;
    }

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

    public RedisCacheService(RedisTemplate redisTemplate, String namespace){
        this.redisTemplate = redisTemplate;
        this.namespace = namespace;
    }

    public String getNamespace(){
        return this.namespace;
    }

    @Override
    public void evicts(String... keys) {
        if (keys != null){
            for (String key : keys){
                evict(key);
            }
        }
    }

    @Override
    public void setProvider(Object object) {
        this.redisTemplate = (RedisTemplate) object;
    }

    public Object getProvider(){
        return this.redisTemplate;
    }

    @Override
    public void put(String key, Object t) {
        put(key, t, -1);
    }


    @Override
    public void put(String key, Object t, long expire) {
        redisTemplate.opsForValue().set(getNamespace() + ":" + key, t);
        if (expire > -1) {
            redisTemplate.expire(getNamespace() + ":" + key, expire, TimeUnit.MILLISECONDS);
        }
    }

    @Override
    public void putHash(String key, String hashKey, Object object) {
        try {
            redisTemplate.opsForHash().put(getNamespace() + ":" + key, hashKey, object);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean expire(String key, long time) {
        try {
            if (time > -1) {
                redisTemplate.expire(getNamespace() + ":" + key, time, TimeUnit.MILLISECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(getNamespace() + ":" + key);
    }

    @Override
    public boolean hasKey(String key, String hashKey) {
        String newKey = getNamespace() + ":" + key;
        Object obj = getByFullKey(newKey);
        if(obj instanceof Map){
            return ((Map)obj).containsKey(hashKey);
        }
        return redisTemplate.opsForHash().hasKey(newKey, hashKey);
    }

    @Override
    public void evict(String key) {
        evictByFullKey(getNamespace() + ":" + key);
    }



    @Override
    public void evictHash(String key, String hashKey) {
        //redis template hashset 与 map 是由区别的。
        redisTemplate.opsForHash().delete(getNamespace() + ":" + key, hashKey);
    }


    public  List list() {
        String prefix = getNamespace();
        List list = new ArrayList();
        Set keys  =  redisTemplate.keys(prefix + ":*");
        for(String key : keys){
            if(isFirstLevelKey(key, prefix)){
                T t = (T)getByFullKey(key);
                if(t != null) {
                    list.add(t);
                }
            }
        }
        return list;
    }

    @Override
    public int count(){
        String prefix = getNamespace();
        Set keys  =  redisTemplate.keys(prefix + ":*");
        int j = 0;
        for(String key : keys){
            if(isFirstLevelKey(key, prefix)){
                Object t = getByFullKey(key);
                if(t != null) {
                    j++;
                }
            }
        }
        return j;
    }

    @Override
    public long size() {
        String prefix = getNamespace();
        Long size = 0L;
        Set keys  =  redisTemplate.keys(prefix + ":*");
        RedisConnectionFactory factory = redisTemplate.getConnectionFactory();

        RedisConnection connection = factory.getConnection();
        Jedis jedis = null;
        if(connection instanceof LettuceConnection){
            LettuceConnection conn = (LettuceConnection)connection;
            for(String key : keys){
                RedisFuture s = conn.getNativeConnection().debugObject(key.getBytes());
                try {
                    size += RedisUtils.getObjectSize(s.get());
                } catch (InterruptedException e) {

                } catch (ExecutionException e) {

                }
            }
//            ((LettuceConnection) conn).getConnection().debugObject()
        }else if(connection instanceof JedisConnection){
            JedisConnection conn = (JedisConnection)connection;
            jedis = conn.getJedis();
            for(String key : keys){
                String s = jedis.debug(DebugParams.OBJECT(key));
                size += RedisUtils.getObjectSize(s);
            }

        }
        connection.close();
        return size;
    }

    @Override
    public void clear() {
        String prefix = getNamespace();
        Set keys  =  redisTemplate.keys(prefix + ":*");
        for(String key : keys){
            if(isFirstLevelKey(key, prefix)){
                evictByFullKey(key);
            }
        }
    }

    public void evictByFullKey(String key){
        redisTemplate.delete(key);
//        redisTemplate.execute(new RedisCallback() {
//            public Long doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                return connection.del(key.getBytes());
//            }
//        });
    }


    @Override
    public  T get(String key){
        return (T) getByFullKey(getNamespace() + ":" + key);
    }

    private Object getByFullKey(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        Object obj = null;
        try {
            obj = redisTemplate.opsForValue().get(key);
        }catch (Exception e){
            Map data = redisTemplate.opsForHash().entries(key);
            if(data != null){
                Map temp = new HashMap();
                for(Object k : data.keySet()){
                    temp.put(k.toString(), data.get(k));
                }
                obj = temp;
            }
        }
        return obj;
    }


    public  T get(String key, String hashKey) {
        String newKey = getNamespace() + ":" + key;
        try{
            return (T)redisTemplate.opsForHash().entries(newKey).get(hashKey);
        }catch (Exception e){
            Object obj = redisTemplate.opsForValue().get(newKey);
            if(obj instanceof Map){
                return (T)((Map)obj).get(key);
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy