SpringBoot 入门之三:写入数据到Redis

 实现存储key value到Redis的类 

package com.xiaour.spring.boot.service.impl;

import com.xiaour.spring.boot.service.RedisService;
import com.xiaour.spring.boot.utils.JsonUtil;
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.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

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


@Service("redisService")
@Transactional(rollbackFor = Exception.class)
public class RedisServiceImpl implements RedisService {

	private static int seconds = 3600 * 24;

	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public boolean set(final String key, final String value) throws Exception {
		Assert.hasText(key, "Key is not empty.");
		boolean result = redisTemplate.execute(new RedisCallback() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();
				connection.set(serializer.serialize(key), serializer.serialize(value));
				return true;
			}
		});
		return result;
	}

	public String get(final String key) throws Exception {
		Assert.hasText(key, "Key is not empty.");
		String result = redisTemplate.execute(new RedisCallback() {
			@Override
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();
				byte[] value = connection.get(serializer.serialize(key));
				return serializer.deserialize(value);
			}
		});
		return result;
	}

	public void del(final String key) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		redisTemplate.execute(new RedisCallback() {
			@Override
			public Long doInRedis(RedisConnection conn) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();
				return conn.del(serializer.serialize(key));
			}
		});
	}

	@Override
	public boolean expire(final String key, long expire) {
		return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
	}

	@Override
	public  boolean setList(String key, List list) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		String value = JsonUtil.getJsonString(list);
		return set(key, value);
	}

	@Override
	public  List getList(String key, Class clz) throws Exception {

		Assert.hasText(key, "Key is not empty.");

		String json = get(key);
		if (json != null) {
			List list = JsonUtil.readJson2Array(json, clz);
			return list;
		}
		return null;
	}

	@Override
	public long lpush(final String key, Object obj) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		final String value = JsonUtil.getJsonString(obj);
		long result = redisTemplate.execute(new RedisCallback() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();
				long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));
				return count;
			}
		});
		return result;
	}

	@Override
	public long rpush(final String key, Object obj) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		final String value = JsonUtil.getJsonString(obj);
		long result = redisTemplate.execute(new RedisCallback() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();
				long count = connection.rPush(serializer.serialize(key), serializer.serialize(value));
				return count;
			}
		});
		return result;
	}

	@Override
	public void hmset(String key, Object obj) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		Map data = JsonUtil.readJsonByteMap(JsonUtil.getJsonString(obj));
		redisTemplate.execute(new RedisCallback() {
			@Override
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();
				connection.hMSet(serializer.serialize(key), data);
				return "";
			}
		});
	}

	@Override
	public  T hget(String key, Class clz) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		return redisTemplate.execute(new RedisCallback() {

			@Override
			public T doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();

				Map result;

				Map data = connection.hGetAll(serializer.serialize(key));
				result = new HashMap<>();
				for (Map.Entry entry : data.entrySet()) {
					result.put(serializer.deserialize(entry.getKey()), serializer.deserialize(entry.getValue()));
				}

				return JsonUtil.json2Obj(JsonUtil.getJsonString(result), clz);
			}
		});
	}

	@Override
	public  List hmGetAll(String key, Class clz) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		List> dataList = new ArrayList<>();
		return redisTemplate.execute(new RedisCallback>() {
			@Override
			public List doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();

				Set keysSet = redisTemplate.keys(key);
				Map data;
				Map result;
				for (String newKey : keysSet) {
					data = connection.hGetAll(serializer.serialize(newKey));
					result = new HashMap<>();
					for (Map.Entry entry : data.entrySet()) {
						result.put(serializer.deserialize(entry.getKey()), serializer.deserialize(entry.getValue()));
					}
					dataList.add(result);
				}
				return JsonUtil.readJson2Array(JsonUtil.getJsonString(dataList), clz);
			}
		});
	}

	@Override
	public String lpop(final String key) throws Exception {
		Assert.hasText(key, "Key is not empty.");

		String result = redisTemplate.execute(new RedisCallback() {
			@Override
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer serializer = redisTemplate.getStringSerializer();
				byte[] res = connection.lPop(serializer.serialize(key));
				return serializer.deserialize(res);
			}
		});
		return result;
	}
}

 

启动Redis:

D:\DevWork\tools\Redis-x64-3.2.100>redis-server.exe

查看缓存:

D:\DevWork\tools\Redis-x64-3.2.100>redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379>
127.0.0.1:6379> set string 'Hello'
OK
127.0.0.1:6379> get Hello
(nil)
127.0.0.1:6379> get string
"Hello"
127.0.0.1:6379>
127.0.0.1:6379> get string
"Hello"
127.0.0.1:6379> get Hello
"99999"
127.0.0.1:6379> get Shawn
"0000"

 

你可能感兴趣的:(JAVA开发工具)