上一篇: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");
}
}