package com.icloud.cache.service.impl;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.icloud.cache.service.IRedisService;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
@Service
public class RedisServiceImpl implements IRedisService {
private static final Logger LOGGER = Logger.getLogger(RedisServiceImpl.class);
@Autowired
private JedisPool pool;
@Override
public String get(String key) {
Jedis jedis = null;
String value = null;
try {
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return value;
}
@Override
public Set<String> getByPre(String pre) {
Jedis jedis = null;
Set<String> value = null;
try {
jedis = pool.getResource();
value = jedis.keys(pre + "*");
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return value;
}
@Override
public String set(String key, String value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.set(key, value);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return "0";
} finally {
returnResource(pool, jedis);
}
}
@Override
public String set(String key, String value, int expire) {
Jedis jedis = null;
try {
jedis = pool.getResource();
int time = jedis.ttl(key).intValue() + expire;
String result = jedis.set(key, value);
jedis.expire(key, time);
return result;
} catch (Exception e) {
LOGGER.error(e.getMessage());
return "0";
} finally {
returnResource(pool, jedis);
}
}
@Override
public Long delPre(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
Set<String> set = jedis.keys(key + "*");
int result = set.size();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String keyStr = it.next();
jedis.del(keyStr);
}
return new Long(result);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return 0L;
} finally {
returnResource(pool, jedis);
}
}
@Override
public Long del(String... keys) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.del(keys);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return 0L;
} finally {
returnResource(pool, jedis);
}
}
@Override
public Long append(String key, String str) {
Jedis jedis = null;
Long res = null;
try {
jedis = pool.getResource();
res = jedis.append(key, str);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return 0L;
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.exists(key);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return false;
} finally {
returnResource(pool, jedis);
}
}
@Override
public Long setnx(String key, String value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.setnx(key, value);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return 0L;
} finally {
returnResource(pool, jedis);
}
}
@Override
public String setex(String key, String value, int seconds) {
Jedis jedis = null;
String res = null;
try {
jedis = pool.getResource();
res = jedis.setex(key, seconds, value);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public Long setrange(String key, String str, int offset) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.setrange(key, offset, str);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return 0L;
} finally {
returnResource(pool, jedis);
}
}
@Override
public List<String> mget(String... keys) {
Jedis jedis = null;
List<String> values = null;
try {
jedis = pool.getResource();
values = jedis.mget(keys);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return values;
}
@Override
public String mset(String... keysvalues) {
Jedis jedis = null;
String res = null;
try {
jedis = pool.getResource();
res = jedis.mset(keysvalues);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public Long msetnx(String... keysvalues) {
Jedis jedis = null;
Long res = 0L;
try {
jedis = pool.getResource();
res = jedis.msetnx(keysvalues);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public String getset(String key, String value) {
Jedis jedis = null;
String res = null;
try {
jedis = pool.getResource();
res = jedis.getSet(key, value);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public String getrange(String key, int startOffset, int endOffset) {
Jedis jedis = null;
String res = null;
try {
jedis = pool.getResource();
res = jedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public Long incr(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = pool.getResource();
res = jedis.incr(key);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public Long incrBy(String key, Long integer) {
Jedis jedis = null;
Long res = null;
try {
jedis = pool.getResource();
res = jedis.incrBy(key, integer);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}
@Override
public Long decr(
m0_72731342
- 粉丝: 4
- 资源: 1829
最新资源
- java毕设项目之ssm线上旅行信息管理系统ssm+vue(完整前后端+说明文档+mysql+lw).zip
- 黑马最新Hive存储压缩以及Hive3性能优化PPT
- java毕设项目之ssm新生报到系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm小学生课外知识学习网站+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm校园美食交流系统+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学生公寓管理中心系统的设计与实现+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学校运动会信息管理系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学生请假系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm医院门诊挂号系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学院党员管理系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm亚盛汽车配件销售业绩管理统+jsp(完整前后端+说明文档+mysql+lw).zip
- 教师教学质量评价系统项目源代码全套技术资料.zip
- java毕设项目之ssm在线医疗服务系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm游戏攻略网站的设计与实现+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm医院住院管理系统+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm在线云音乐系统的设计与实现+jsp(完整前后端+说明文档+mysql+lw).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈