Redis存取工具

一、上代码

package cn.stylefeng.guns.core.util;

import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.*;

@Component
public class RedisUtil {
	@Autowired
	private RedisTemplate redisTemplate;

	public static RedisUtil redisUtil;


	@PostConstruct
	public void init(){
		redisUtil = this;
	}
	/**
	 * 设置key,Value-String
	 * 
	 * @param key
	 * @param value
	 */
	public void set(final String key, final String value) {
		redisUtil.redisTemplate.opsForValue().set(key, value);
		/*redisTemplate.execute(new RedisCallback() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.set(key.getBytes(), value.getBytes());
				return 1L;
			}
		});*/
	}

	public void setForList(final String key, final List value) {
		redisUtil.redisTemplate.opsForValue().set(key, value);
		/*redisTemplate.execute(new RedisCallback() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.set(key.getBytes(), value.getBytes());
				return 1L;
			}
		});*/
	}
 
	/**
	 * 设置key(有过期时间)
	 * 
	 * @param key
	 * @param value
	 */
	public void set(final String key, final String value, final int second) {
		redisUtil.redisTemplate.execute(new RedisCallback() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.setEx(key.getBytes(), second, value.getBytes());
				return 1L;
			}
		});
	}
 
	/**
	 * 判断key是否存在
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return (boolean) redisUtil.redisTemplate.execute(new RedisCallback() {
			public Object doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.exists(key.getBytes());
			}
		});
	}
	/**
	 * 删除key
	 * 
	 * @param key
	 * @param value
	 */
	public void del(final String key) {
		redisUtil.redisTemplate.execute(new RedisCallback() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.del(key.getBytes());
				return 1L;
			}
		});
	}
	/**
	 * 通过key获取value
	 *
	 * @param key
	 * @return
	 */
	public String get(final String key) {
		Object obj = redisUtil.redisTemplate.execute(new RedisCallback() {
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				byte[] values = connection.get(key.getBytes());
				if (ToolUtil.isNotEmpty(values)) {
					return new String(values);
				}
				return "";
			}
		});
		if(ToolUtil.isNotEmpty(obj)){
			return (String)obj;
		}else {
			return "";
		}

	}

	public List getForList(final String key) {
		return (List)redisUtil.redisTemplate.opsForValue().get(key);
		/*return (String) redisTemplate.execute(new RedisCallback() {
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				byte[] values = connection.get(key.getBytes());
				if (null != values) {
			    	return new String(values);
				}
				return "";
			}
		});*/
	}
 
	/**
	 * 通过key + field获取value
	 * 
	 * @param key,field
	 * @return
	 */
	public String hget(final String key, final String field) {
		return (String) redisUtil.redisTemplate.execute(new RedisCallback() {
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				byte[] values = connection.hGet(key.getBytes(), field.getBytes());
				if (null != values) {
					return new String(values);
				}
				return "";
			}
		});
	}
 
	/**
	 * 通过key + field设置value
	 * 
	 * @param key,field
	 * @return
	 */
	public boolean hset(final String key, final String field, final String value) {
		return (boolean) redisUtil.redisTemplate.execute(new RedisCallback() {
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.hSet(key.getBytes(), field.getBytes(), value.getBytes());
			}
		});
	}
 
	/**
	 * 模糊查找所有的key
	 * 
	 * @param key
	 * @param value
	 */
	public Set<String> keys(final String keyRegex) {
		return redisUtil.redisTemplate.keys(keyRegex);
	}
	/**
	 * 通过key 获取所有的field +value
	 * 
	 * @param key
	 * @return
	 */
	public Map<String, String> hgetall(final String key) {
		return (Map<String, String>) redisUtil.redisTemplate.execute(new RedisCallback() {
			public Map<String, String> doInRedis(RedisConnection connection) throws DataAccessException {
				Map<byte[], byte[]> values = connection.hGetAll(key.getBytes());
				if (CollectionUtils.isEmpty(values)) {
					return new HashMap<String, String>();
				} else {
					// 将Map<byte[],byte[]>转换为Map<String,String>
					Map<String, String> allValueMap = new HashMap<String, String>(values.size());
					Iterator itor = values.keySet().iterator();
					while (itor.hasNext()) {
						byte[] mapKey = (byte[]) itor.next();
						allValueMap.put(new String(mapKey), new String(values.get(mapKey)));
					}
					return allValueMap;
				}
			}
		});
	}
 
	/**
	 * list push
	 * 
	 * @param key
	 * @param value
	 */
	public void rpush(final String key, final String value) {
		redisUtil.redisTemplate.execute(new RedisCallback() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.rPush(key.getBytes(), value.getBytes());
				return 1L;
			}
		});
	}
 
	/**
	 * list get range
	 * 
	 * @param key
	 * @param value
	 */
	public List<String> lrange(final String key, final long begin, final long end) {
		return (List<String>) redisUtil.redisTemplate.execute(new RedisCallback() {
			public List<String> doInRedis(RedisConnection connection) throws DataAccessException {
				List<byte[]> result = connection.lRange(key.getBytes(), begin, end);
				List<String> resultList = new ArrayList<>();
				for(byte[] r : result){//将字节数组转换为字符串数组
					resultList.add(new String(r));
				}
				return resultList;
			}
		});
	}
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于双瑜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值