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

com.hframework.common.client.redis.RedisService Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.hframework.common.client.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hframework.common.util.message.JsonUtils;
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;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.IOException;
import java.util.*;

/**
 * User: zhangqh6
 * Date: 2016/2/18 15:36:36
 */
public class RedisService {
    private RedisTemplate redisTemplate;

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

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

    protected RedisSerializer getRedisSerializer() {
        return redisTemplate.getStringSerializer();
    }


    /**
     * 添加对象
     * @param key
     * @param t
     * @param 
     * @return
     */
    public  Boolean add(final String key, final T t) {
        return (Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(String.valueOf(key));
                String value = null;
                try {
                    value = JsonUtils.writeValueAsString(t);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                byte[] json = serializer.serialize(value);
                return connection.setNX(keybt, json);
            }
        });
    }

    /**
     * 修改
     * @param key
     * @param t
     * @param 
     * @return
     */
    public  boolean update(final String key, final T t) {
        if (!contains(key)) {
            throw new NullPointerException("数据行不存在, key = " + key);
        }
        return (Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keyTemp = serializer.serialize(key);
                String value = null;
                try {
                    value = JsonUtils.writeValueAsString(t);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                byte[] json = serializer.serialize(value);
                connection.set(keyTemp, json);
                return true;
            }
        });
    }

    /**
     * 保存或更新
     * @param key
     * @param t
     * @param 
     * @return
     */
    public  boolean saveOrUpdate(final String key, final T t) {
        if (!contains(key)) {
            //如果值不存在,则添加入库
            return this.add(key, t);
        }
        return (Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keyTemp = serializer.serialize(key);
                String value = null;
                try {
                    value = JsonUtils.writeValueAsString(t);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                byte[] json = serializer.serialize(value);
                connection.set(keyTemp, json);
                return true;
            }
        });
    }

    /**
     * 保存或更新
     * @param key
     * @param t
     * @param 
     * @return
     */
    public  boolean saveOrUpdate(final String key, final T t, final Long seconds) {
        if (!contains(key)) {
            //如果值不存在,则添加入库
            return this.add(key, t);
        }
        return (Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keyTemp = serializer.serialize(key);
                String value = null;
                try {
                    value = JsonUtils.writeValueAsString(t);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                byte[] json = serializer.serialize(value);
                connection.setEx(keyTemp, seconds, json);
                return true;
            }
        });
    }

    /**
     * map, 值递增
     * @param key
     * @param field
     * @param delta
     * @param 
     * @return
     */
    public  boolean hIncrBy(final String key, final T field,final Long delta) {
        return (Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keyTemp = serializer.serialize(key);
                byte[] fieldTemp = serializer.serialize(String.valueOf(field));
                connection.hIncrBy(keyTemp, fieldTemp, delta);
                return true;
            }
        });
    }

    /**
     * 删除
     * @param key
     */
    public void delete(String key) {
        List list = new ArrayList();
        list.add(key);
        delete(list);
    }
    /**
     * 批量删除
     * @param keys
     */
    public void delete(List keys) {
        redisTemplate.delete(keys);
    }

    public Boolean contains(final String key) {
        Boolean result = (Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(key);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return false;
                }
                return true;
            }
        });
        return result;
    }
    public  T get(final String key, final Class... cls) {
        T result = (T) redisTemplate.execute(new RedisCallback() {
            public T doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(key);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                try {
                    return (T) JsonUtils.readValue(serializer.deserialize(value),cls);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
        return result;
    }

    public  T get(final String key, final Class cls) {
        T result = (T) redisTemplate.execute(new RedisCallback() {
            public T doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(key);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                try {
                    return JsonUtils.readValue(serializer.deserialize(value),cls);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
        return result;
    }

    /**
     * 查询字符串
     * @param keyId
     * @return
     */
    public String getAsString(final String keyId) {
        String result = (String) redisTemplate.execute(new RedisCallback() {
            public String doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(keyId);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                String mjson = serializer.deserialize(value);
                return mjson;
            }
        });
        return result;
    }

    /**
     * 查询Long对象
     * @param keyId
     * @return
     */
    public long getAsLong(final String keyId) {
        Long result = (Long) redisTemplate.execute(new RedisCallback() {
            public Long doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(keyId);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                return Long.valueOf(serializer.deserialize(value));
            }
        });
        return result;
    }


    public  List getList(final String key, final Class cls) {
        List result = (List) redisTemplate.execute(new RedisCallback>() {
            public List doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(key);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                String mjson = serializer.deserialize(value);
                return JSON.parseArray(mjson, cls);
            }
        });
        return result;
    }


    public  Map getMap(final String key, final Class cls) {
        Map result = (Map) redisTemplate.execute(new RedisCallback>() {
            public Map doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(key);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                Map resultMap = new HashMap();
                String mjson = serializer.deserialize(value);
                if (mjson != null) {
                    JSONObject jsonObject = JSON.parseObject(mjson);
                    Iterator iterator = jsonObject.keySet().iterator();
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        String val = jsonObject.getString(key);
                        K k = (K) key;
                        T t = (T) JSON.parseObject(val, cls);
                        resultMap.put(k, t);
                    }
                }
                return resultMap;
            }
        });
        return result;
    }

    public  Map> getMapList(final String key, final Class cls) {
        Map> result = (Map>) redisTemplate.execute(new RedisCallback>>() {
            @SuppressWarnings("unchecked")
            public Map> doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(key);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                Map> resultMap = new HashMap>();
                String mjson = serializer.deserialize(value);
                if (mjson != null) {
                    JSONObject jsonObject = JSON.parseObject(mjson);
                    Iterator iterator = jsonObject.keySet().iterator();
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        String val = jsonObject.getString(key);
                        K k = (K) key;
                        List t = (List) JSON.parseArray(val, cls);
                        resultMap.put(k, t);
                    }
                }
                return resultMap;
            }
        });
        return result;
    }

    public  Map> getMapTree(final String key, final Class cls) {
        Map> result = (Map>) redisTemplate.execute(new RedisCallback>>() {
            @SuppressWarnings("unchecked")
            public Map> doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer serializer = getRedisSerializer();
                byte[] keybt = serializer.serialize(key);
                byte[] value = connection.get(keybt);
                if (value == null) {
                    return null;
                }
                Map> resultMap = new HashMap>();
                String mjson = serializer.deserialize(value);
                if (mjson != null) {
                    JSONObject jsonObject = JSON.parseObject(mjson);
                    Iterator iterator = jsonObject.keySet().iterator();
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        String val = jsonObject.getString(key);
                        K k = (K) key;
                        JSONObject jsonObjectTemp = JSON.parseObject(val);
                        Iterator iteratorTemp = jsonObjectTemp.keySet().iterator();
                        Map map = new HashMap();
                        while (iteratorTemp.hasNext()) {
                            String keyTemp = iteratorTemp.next();
                            String valTemp = jsonObjectTemp.getString(keyTemp);
                            K ktemp = (K) keyTemp;
                            T ttemp = JSON.parseObject(valTemp, cls);
                            map.put(ktemp, ttemp);
                        }
                        resultMap.put(k, map);
                    }
                }
                return resultMap;
            }
        });
        return result;
    }

    /**
     * 获取redis map key
     * @param cls
     * @return
     */
    public String getRedisMapKey(Class cls){
        return cls.asSubclass(cls).getSimpleName() + "_map";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy