package com.oaec.shitou.cache.impl;
import java.io.Serializable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.core.ValueOperations;
import org.springframework.stereotype.Repository;
import com.oaec.shitou.cache.RedisDao;
import com.oaec.shitou.util.SerializeUtil;
@Repository("redisDao")
public class RedisDaoImpl implements RedisDao {
Log log = LogFactory.getLog(RedisDaoImpl.class);
@Autowired
private RedisTemplate<Serializable, Serializable> redisTemplate;
private long expriseTime=600;//redis 澶辨晥鏃堕棿绉�
@Override
public void set(final String key, final String value) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.setEx(
redisTemplate.getStringSerializer().serialize(key),
expriseTime,
redisTemplate.getStringSerializer().serialize(value));
return null;
}
});
}
@Override
public String get(final String key) {
return redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = redisTemplate.getStringSerializer()
.serialize(key);
if (connection.exists(keys)) {
byte[] values = connection.get(keys);
String value = redisTemplate.getStringSerializer()
.deserialize(values);
return value;
}
return null;
}
});
}
@Override
public Object getObject(final String key) {
log.info("getObject key:" + key);
return redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = redisTemplate.getStringSerializer()
.serialize(key);
if (connection.exists(keys)) {
byte[] values = connection.get(keys);
return SerializeUtil.unserialize(values);
}
return null;
}
});
}
@Override
public void delete(final String key) {
log.info("delete key:" + key);
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
long count = connection.del(
redisTemplate.getStringSerializer().serialize(key));
System.out.println("redis delete" + count);
return null;
}
});
}
@Override
public void setObject(final String key,final Object obj) {
log.info("setObject key:" + key);
final byte[] objByte = SerializeUtil.serialize(obj);
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.setEx(
redisTemplate.getStringSerializer().serialize(key),
expriseTime,
objByte);
return null;
}
});
}
@Override
public void setString(String key, String value) {
ValueOperations<Serializable, Serializable> opsForValue = redisTemplate.opsForValue();
opsForValue.set("name", value);
}
@Override
public void setEntity(String key, String obj) {
ValueOperations<Serializable, Serializable> opsForValue = redisTemplate.opsForValue();
opsForValue.set(key, obj);
}
@Override
public String getString(String key) {
redisTemplate.getValueSerializer();
return null;
}
@Override
public Object getEntity(String key) {
return null;
}
@Override
public void del(String uid) {
// TODO Auto-generated method stub
}
}
最近下载更多
2036495585 LV9
2023年9月25日
wcm2003 LV1
2023年6月3日
lwp011 LV27
2022年9月19日
brucega LV3
2022年6月1日
yuqi886 LV5
2022年4月16日
大水池 LV2
2022年4月8日
Yee. LV5
2022年3月13日
刘123456789 LV8
2022年2月24日
wanglinddad LV55
2022年1月7日
1211366946 LV3
2021年12月27日
最近浏览更多
奋斗的小蚂蚁 LV14
10月17日
ES大兵 LV11
6月13日
cq1458824624
1月14日
暂无贡献等级
xinshou11111x
2024年12月17日
暂无贡献等级
bluerstar LV1
2024年10月23日
orilore LV2
2024年10月10日
微信网友_7151914139078656 LV2
2024年9月4日
799743530 LV11
2024年7月10日
quartz LV8
2024年7月1日
123456cjj LV1
2024年6月2日

