Java封装redis工具类

首先感谢我浩哥 我只是个搬运工 将文档记录在这里方便使用

本人的项目是springboot框架
源码:String类型


import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * redicache 工具类
 * 
 */
@SuppressWarnings("unchecked")
@Component
public class RedisUtil {
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 批量删除对应的value
	 * 
	 * @param keys
	 */
	public void remove(final String... keys) {
		for (String key : keys) {
			remove(key);
		}
	}

	/**
	 * 批量删除key
	 * 
	 * @param pattern
	 */
	public void removePattern(final String pattern) {
		Set keys = redisTemplate.keys(pattern);
		if (keys.size() > 0)
			redisTemplate.delete(keys);
	}
	
	public void removeByPrex(final String pattern) {
		Set keys = redisTemplate.keys(pattern + "*");
		if (keys.size() > 0)
			redisTemplate.delete(keys);
	}
	
	public void removeBySuffix(final String pattern) {
		Set keys = redisTemplate.keys("*" + pattern);
		if (keys.size() > 0)
			redisTemplate.delete(keys);
	}

	/**
	 * 删除对应的value
	 * 
	 * @param key
	 */
	public void remove(final String key) {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}

	/**
	 * 判断缓存中是否有对应的value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}

	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public Object get(final String key) {
		Object result = null;
		ValueOperations operations = redisTemplate.opsForValue();
		result = operations.get(key);
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
		boolean result = false;
		try {
			ValueOperations operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value, Long expireTime) {
		boolean result = false;
		try {
			ValueOperations operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	    public boolean setHash(final String key, String hashKey,String value){
	    	boolean result = false;
	    	try {
	    		HashOperations operations = redisTemplate.opsForHash();
		        operations.put(key, hashKey, value);
		        result = true;
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	return result;
	        
	    }
	    public String getHash(final String key, String hashKey){
	    	String value = null;
	    	try {
	    		HashOperations operations = redisTemplate.opsForHash();
		        value = operations.get(key, hashKey);
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	return value;
	    }
	    
	    public List getHash(final String key){
	    	List list = null;
	    	try {
	    		HashOperations operations = redisTemplate.opsForHash();
	    		list = operations.values(key);
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	return list;
	    }
}

List类型(list类型存入链表结构 取数据时可以按下标取值 redis中保存json类型)


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
@SuppressWarnings("unchecked")
@Repository
public class ListRedisTemplate {
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;


    /** 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
	public  Long lpush(String key, T t) {  
        return redisTemplate.opsForList().leftPush(key, t);  
        
    }  
  
    /** 
     *  
     * @param key 
     * @return 
     */  
    public  T lpop(String key) {  
        return (T) redisTemplate.opsForList().leftPop(key);  
    }  
  
    /** 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public  Long rpush(String key, T t) {  
        return redisTemplate.opsForList().rightPush(key, t);  
    }  
  
    /** 
     *  
     * @param key 
     * @return 
     */  
    public  T rpush(String key) {  
        return (T) redisTemplate.opsForList().leftPop(key);  
    }  
  
    /** 
     *  
     * @param key 
     * @return 
     */  
    public Long length(String key) {  
        return redisTemplate.opsForList().size(key);  
    }  
  
    /** 
     *  
     * @param key 
     * @param start 
     * @param end 
     * @return 
     */  
    public  List range(String key, int start, int end) {  
        return redisTemplate.opsForList().range(key, start, end);  
    }  
  
    /** 
     *  移除key中值为value的i个,返回删除的个数
     *  
     * @param key 
     * @param i 
     * @param value 
     */  
    public Long remove(String key, long i, String value) {  
        return redisTemplate.opsForList().remove(key, i, value);  
    }  
  
    /** 
     * 检索 
     *  
     * @param key 
     * @param index 
     * @return 
     */  
    public  T index(String key, long index) {  
        return (T) redisTemplate.opsForList().index(key, index);  
    }  
  
    /** 
     * 赋值 
     *  
     * @param key 
     * @param index 
     * @param value 
     */  
    public  void set(String key, long index, T t) {  
        redisTemplate.opsForList().set(key, index, t);  
    }  
  
    /** 
     * 裁剪,删除除了[start,end]以外的所有元素
     *  
     * @param key 
     * @param start 
     * @param end 
     */  
    public void trim(String key, long start, int end) {  
        redisTemplate.opsForList().trim(key, start, end);  
    }  
}

set类型

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
@Repository
public class SetRedisTemplate {
	@SuppressWarnings("rawtypes")
	@Autowired
    private RedisTemplate redisTemplate;
 
    
    /**
     * 新增
     * @param key
     * @param t
     * @return
     */
	
	public  Long add(String key, T... t) {  
        return redisTemplate.opsForSet().add(key, t);
    }  
 
    
    /**
     * 获取set
     * 
     * @param key
     * @return
     */
	public  Set  members(String key){
        return redisTemplate.opsForSet().members(key);
    }
 
	/**
	 * 从redis的某个key下面的set集合中删除元素。
	 * @param key
	 * @return
	 */
	public   Long remove(String key, T... t) {
		return redisTemplate.opsForSet().remove(key, t);
    }
	
	/**
	 * 查看是否存在指定数据
	 * @param key
	 * @param t
	 * @return
	 */
	public   Boolean isMember(String key,T t){
        return redisTemplate.opsForSet().isMember(key, t);
    }
	
	public  Long size(String key){
        return redisTemplate.opsForSet().size(key);
    }
	
	/**
	 * 移除随机
	 * @param key
	 * @return
	 */
	public  T pop(String key){
        return (T) redisTemplate.opsForSet().pop(key);
    }
	
}

hash类型


import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
@Repository
public class HashRedisTemplate {
	@SuppressWarnings("rawtypes")
	@Autowired
    private RedisTemplate redisTemplate;
 
    /**
     * 添加单个
     *
     * @param key    key
     * @param filed  filed
     * @param domain 对象
     */
    
    public  void put(String key,String filed,T domain){
        redisTemplate.opsForHash().put(key, filed, domain);
    }
 
 
    /**
     * 添加HashMap
     *
     * @param key    key
     * @param hm    要存入的hash表
     */
    
    public  void mput(String key, HashMap hm){
        redisTemplate.opsForHash().putAll(key,hm);
    }
 
    /**
     * 查询key和field所确定的值
     *
     * @param key 查询的key
     * @param field 查询的field
     * @return HV
     */
    
	public  T get(String key,String field) {
        return (T) redisTemplate.opsForHash().get(key, field);
    }
 
    /**
     * 查询该key下所有值
     *
     * @param key 查询的key
     * @return Map
     */
    
	public  Map mget(String key) {
        return  (Map) redisTemplate.opsForHash().entries(key);
    }
 
    /**
     * 删除key下所有值
     *
     * @param key 查询的key
     */
    
    public void delete(String key) {
        redisTemplate.opsForHash().getOperations().delete(key);
    }
 
    /**
     * 判断key和field下是否有值
     *
     * @param key 判断的key
     * @param field 判断的field
     */
    
    public Boolean hasKey(String key,String field) {
        return redisTemplate.opsForHash().hasKey(key,field);
    }
 
    /**
     * 判断key下是否有值
     *
     * @param key 判断的key
     */
    
    public Boolean hasKey(String key) {
        return redisTemplate.opsForHash().getOperations().hasKey(key);
    }
    
}


import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
@Repository
public class ValueRedisTemplate {
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;
	// StringRedisTemplate stringRedisTemplate;

	/**
	 * 查询key,支持模糊查询
	 *
	 * @param key
	 *            传过来时key的前后端已经加入了*,或者根据具体处理
	 */

	public  Set keys(String key) {
		return redisTemplate.keys(key);
	}

	/**
	 * 重命名key
	 */

	public  void renameKey(String key, T t) {
		redisTemplate.rename(key, t);
	}

	/**
	 * 字符串添加信息
	 * 
	 * @param key
	 * @param t
	 *            可以是单个的值,也可以是任意类型的对象
	 */

	public  void set(String key, T t) {
		redisTemplate.opsForValue().set(key, t);
	}

	/**
	 * 字符串添加信息
	 * 
	 * @param key
	 * @param t
	 *            可以是单个的值,也可以是任意类型的对象
	 * @param expire
	 *            设置失效时间
	 */

	public  void set(String key, T t, long expire) {
		redisTemplate.opsForValue().set(key, t, expire, TimeUnit.SECONDS);
	}

	/**
	 * 字符串添加信息
	 * 
	 * @param key
	 * @param t
	 *            可以是单个的值,也可以是任意类型的对象
	 * @return
	 */

	public  Boolean setIfAbsent(String key, T t) {
		return redisTemplate.opsForValue().setIfAbsent(key, t);
	}

	/**
	 * 字符串获取值
	 * 
	 * @param key
	 */

	public  T get(String key) {
		return (T) redisTemplate.opsForValue().get(key);
	}

	public  T getAndSet(String key, T value) {
		return (T) redisTemplate.opsForValue().getAndSet(key, value);
	}

	public void delete(String key) {
		redisTemplate.opsForValue().getOperations().delete(key);
	}

	public Boolean expire(String key, long sec) {
		return redisTemplate.expire(key, sec, TimeUnit.SECONDS);
	}

	public Boolean expireAt(String key, Date date) {
		return redisTemplate.expireAt(key, date);
	}

}

你可能感兴趣的:(Java封装redis工具类)