springmvc整合redis(补充一)

上一篇:http://blog.csdn.net/h996666/article/details/78124232

上一篇接口写的不太完整,只进行了简单的操作。

这篇补充对对象,JSON,List的添加。以及删除操作。

贴代码:

接口RedisService.java

public interface RedisService {
	
	/**
	 * 添加
	 * 
	 * @param key
	 * @param value
	 */
	void put(String key, Object value);
	
	/**
	 * 添加
	 * 
	 * @param key
	 * @param value
	 * @param expiredTime 过期时间
	 */
	void put(String key, Object value, Long expiredTime);
	
	/**
	 * 获取
	 * 
	 * @param key
	 * @return
	 */
	Object get(String key);
	
	/**
	 * 删除
	 * 
	 * @param key
	 */
	void delete(String key);
}
接口实现RedisServiceImpl.java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.commons.lang3.StringUtils;
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.Service;

import com.service.redis.RedisService;

@Service
public class RedisServiceImpl implements RedisService {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	/**
	 * 添加
	 */
	@Override
	public void put(String key, Object value) {	
		put(key, value, null);
	}

	/**
	 * 添加
	 */
	@Override
	public void put(String key, Object value, Long expiredTime) {
		if(StringUtils.isBlank(key)) {
			return;
		}
		if (null == value) {
			return;
		}
		redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection conn) throws DataAccessException {
				byte[] keyb = key.getBytes();
				byte[] valueb = toByteArray(value);
				conn.set(keyb, valueb);
				if (null != expiredTime) {
					conn.expire(keyb, expiredTime);
				}
				return 1l;
			}
		});
	}

	/**
	 * 获取
	 */
	@Override
	public Object get(String key) {
		Object value;
		value = redisTemplate.execute(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection conn) throws DataAccessException {
				byte[] keyb = key.getBytes();
				byte[] valueb = conn.get(keyb);
				return toObject(valueb);
			}
		});
		return value;
	}
	
	/**
	 * 删除
	 */
	@Override
	public void delete(String key) {
		redisTemplate.execute(new RedisCallback<Long>() {

			@Override
			public Long doInRedis(RedisConnection conn) throws DataAccessException {
				byte[] keyb = key.getBytes();
				conn.del(keyb);
				return 1l;
			}
		});
		
	}

	/**
	 * 将byte[] 转成 Object
	 * 
	 * @param bytes
	 * @return
	 */
	private Object toObject(byte[] bytes) {
		Object obj = null;
		ByteArrayInputStream bais = null;
		ObjectInputStream ois = null;
		try {
			bais = new ByteArrayInputStream(bytes);
			ois = new ObjectInputStream(bais);
			obj = ois.readObject();
		} catch(IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ois.close();
				bais.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}
	
	/**
	 * object 转成 byte[]
	 * @param obj
	 * @return
	 */
	private byte[] toByteArray(Object obj) {
		byte[] bytes = null;
		ByteArrayOutputStream baos = null;
		ObjectOutputStream oos = null;
		try {
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(obj);
			oos.flush();
			bytes = baos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				oos.close();
				baos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bytes;
	}
}
JUNIT测试类:

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.model.user.User;
import com.service.redis.RedisService;

import test.service.BaseTest;

public class RedisServiceTest extends BaseTest {
	@Autowired
	private RedisService redisService;
	
	/**
	 * 添加对象(该对象需要实现Serializable接口)
	 */
	@Test
	public void testPut_1() {
		User user = new User();
		user.setId(1001);
		user.setName("huang");
		redisService.put("user1", user);
	}
	
	/**
	 * 添加JSON对象
	 */
	@Test
	public void testPut_2() {
		JSONObject user = new JSONObject();
		user.put("id", "1002");
		user.put("name", "zhang");
		redisService.put("user1", user);
	}
	
	/**
	 * 添加数组
	 */
	@Test
	public void testPut_3() {
		List<User> userList = new ArrayList<User>();
		User user1 = new User();
		user1.setId(1001);
		user1.setName("huang");
		User user2 = new User();
		user2.setId(1002);
		user2.setName("zhang");
		User user3 = new User();
		user3.setId(1003);
		user3.setName("li");
		
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		redisService.put("userList", userList);
	}
	
	/**
	 * 获取
	 */
	@Test
	public void testGet() {
		System.out.println(redisService.get("userList"));
	}
	
	/**
	 * 删除
	 */
	@Test
	public void testDelete() {
		redisService.delete("name");
		redisService.delete("age");
		redisService.delete("shuzi");
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值