验证中...
片段 1 片段 2 片段 3 片段 4
application.properties
原始数据 复制代码
jedis.pool.host=localhost
jedis.pool.port=6379
#最大能够保持idle状态的对象数
jedis.pool.config.maxIdle=300
#最大分配的对象数
jedis.pool.config.maxTotal=60000
###
jedis.pool.config.maxWaitMillis=100000
#当调用borrow Object方法时,是否进行有效性检查
jedis.pool.config.testOnBorrow=true
RedisConfiguraion.java
原始数据 复制代码
package com.example.redis.jedis;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfiguraion {
@Bean(name = "jedis.pool")
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${jedis.pool.host}") String host,
@Value("${jedis.pool.port}") int port){
return new JedisPool(config,host,port);
}
@Bean(name = "jedis.pool.config")
public JedisPoolConfig jedisPoolConfig(
@Value("${jedis.pool.config.maxTotal}") int maxTotal,
@Value("${jedis.pool.config.maxIdle}") int maxIdle,
@Value("${jedis.pool.config.maxWaitMillis}") int maxWaitMillis,
@Value("${jedis.pool.config.testOnBorrow}") boolean testOnBorrow){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
config.setTestOnBorrow(testOnBorrow);
return config;
}
}
RedisDao.java
原始数据 复制代码
package org.seckill.dao.cache;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Redis Cache 工具类
* 1.优化缓存时间的表达式逻辑 2018-03-24
* 2.通过 protostuff 缓存对象
* @author dhm
* @version 2018-03-28
*/
@Component
public class RedisDao<T>{
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JedisPool jedisPool;
/**
* string(字符串)
* set key value [EX seconds] [PX milliseconds] [NX|XX]
* @param key
* @param value
* @param cacheSeconds
* @return
*/
public String set(String key, String value, int cacheSeconds){
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
if (cacheSeconds > 0) {
result = jedis.setex(key,cacheSeconds,value);
}else{
result = jedis.set(key, value);
}
logger.debug("setString {} = {}", key, value);
} catch (Exception e) {
logger.warn("setString {} = {}", key, value, e);
} finally {
jedis.close();
}
return result;
}
/**
* string(字符串)
* get key
* @param key
* @return
*/
public String get(String key) {
Jedis jedis = null;
String value = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
value = jedis.get(key);
// value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
logger.debug("getString {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getString {} = {}", key, value, e);
} finally {
jedis.close();
}
return value;
}
/**
*
* hash(哈希)
* hmset key field value [field value ...]
* @param key
* @param value
* @param cacheSeconds
* @return
*/
public String hmset(String key, Map<String, String> value, int cacheSeconds) {
String result = null;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
jedis.del(key);
}
result = jedis.hmset(key, value);
if (cacheSeconds > 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setMap {} = {}", key, value);
} catch (Exception e) {
logger.warn("setMap {} = {}", key, value, e);
} finally {
jedis.close();
}
return result;
}
/**
* hash(哈希)
* hgetall key
* @param key
* @return
*/
public Map<String, String> hgetAll(String key) {
Map<String, String> value = null;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
value = jedis.hgetAll(key);
logger.debug("getMap {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getMap {} = {}", key, value, e);
} finally {
jedis.close();
}
return value;
}
/**
* list(列表)
* rpush key value [value ...]
* @param key
* @param value
* @param cacheSeconds
* @return
*/
public long rpush(String key, List<String> value, int cacheSeconds) {
long result = 0;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
jedis.del(key);
}
result = jedis.rpush(key, value.toArray(new String[value.size()]));
if (cacheSeconds > 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setList {} = {}", key, value);
} catch (Exception e) {
logger.warn("setList {} = {}", key, value, e);
} finally {
jedis.close();
}
return result;
}
/**
* list(列表)
* lrange key start stop
* @param key
* @return
*/
public List<String> lrange(String key) {
List<String> value = null;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
value = jedis.lrange(key, 0, -1);
logger.debug("getList {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getList {} = {}", key, value, e);
} finally {
jedis.close();
}
return value;
}
/**
* set(集合)
* sadd key member [member ...]
* @param key
* @param value
* @param cacheSeconds
* @return
*/
public long sadd(String key, Set<String> value, int cacheSeconds) {
long result = 0;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
jedis.del(key);
}
result = jedis.sadd(key, value.toArray(new String[value.size()]));
if (cacheSeconds > 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setSet {} = {}", key, value);
} catch (Exception e) {
logger.warn("setSet {} = {}", key, value, e);
} finally {
jedis.close();
}
return result;
}
/**
* set(集合)
* smembers key
* @param key
* @return
*/
public Set<String> smembers(String key) {
Set<String> value = null;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
value = jedis.smembers(key);
logger.debug("getSet {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getSet {} = {}", key, value, e);
} finally {
jedis.close();
}
return value;
}
/**
* zset(sorted set:有序集合)
* zadd key [NX|XX] [CH] [INCR] score member [score member ...]
* @param key
* @param value
* @param cacheSeconds
* @return
*/
public long zset(String key, Map<String,Double> value, int cacheSeconds){
long result = 0;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
jedis.del(key);
}
result = jedis.zadd(key,value);
if (cacheSeconds > 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setZset {} = {}", key, value);
} catch (Exception e) {
logger.warn("setZset {} = {}", key, value, e);
} finally {
jedis.close();
}
return result;
}
/**
* zset(sorted set:有序集合)
* zrange key min max [WITHSCORES] [LIMIT offset count]
* @param key
* @return
*/
public Set<String> zrange(String key, long min, long max) {
Set<String> value = null;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
if (jedis.exists(key)) {
value = jedis.zrange(key,min,max);
logger.debug("getZset {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getZset {} = {}", key, value, e);
} finally {
jedis.close();
}
return value;
}
public void pipeline(Map<String,String> map){
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
Pipeline pipeline = jedis.pipelined();
for(Map.Entry<String,String> entry:map.entrySet()){
pipeline.set(entry.getKey().getBytes(),entry.getValue().getBytes());
}
pipeline.sync();
}catch (Exception e) {
logger.warn("setPipeline {}", map, e);
} finally {
jedis.close();
}
}
public String setObject(String key,T t,int cacheSeconds){
RuntimeSchema<T> schema = (RuntimeSchema<T>) RuntimeSchema.createFrom(t.getClass());
byte[] bytes = ProtostuffIOUtil.toByteArray(t,
schema,
LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
Jedis jedis = null;
String result = null;
try{
jedis = jedisPool.getResource();
result = jedis.setex(key.getBytes(),cacheSeconds,bytes);
logger.debug("setObject {} = {}", key, t.getClass());
} catch (Exception e) {
logger.warn("setObject {} = {}", key, t.getClass(), e);
} finally {
jedis.close();
}
return result;
}
public T getObject(String key,Class<T> t) {
RuntimeSchema<T> schema = RuntimeSchema.createFrom(t);
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
byte[] bytes = jedis.get(key.getBytes());
//缓存重获取到
if (bytes != null) {
T tt = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes,tt, schema);
//t被反序列化
logger.debug("getObject {} = {}", key, t);
return tt;
}
} catch (Exception e) {
logger.warn("getObject {} = {}", key, t, e);
} finally {
jedis.close();
}
return null;
}
}
RedisApplicationTests.java
原始数据 复制代码
package com.example.redis;
import com.example.redis.jedis.RedisUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {
@Autowired
private RedisUtils redisUtils;
@Test
public void contextLoads() {
//String
String string_result = redisUtils.set("string_key","string_value",0);
String string_value = redisUtils.get("string_key");
//Map
Map<String,String> map = new HashMap<>();
map.put("map_key1","map_value1");
map.put("map_key2","map_value2");
map.put("map_key3","map_value3");
String map_result = redisUtils.hmset("map_key",map,0);
Map<String,String> map_value = redisUtils.hgetAll("map_key");
//List
List<String> list = new ArrayList<>();
list.add("list_value1");
list.add("list_value2");
list.add("list_value3");
list.add("list_value4");
long list_result = redisUtils.rpush("list_key",list,0);
List<String> list_value = redisUtils.lrange("list_key");
//Set
Set<String> set = new HashSet<>();
set.add("set_value1");
set.add("set_value2");
set.add("set_value3");
set.add("set_value4");
long set_result = redisUtils.sadd("set_key",set,0);
Set<String> set_value = redisUtils.smembers("set_key");
//Zset
Map<String,Double> zset = new HashMap<>();
zset.put("map_key1",10.1);
zset.put("map_key2",9.8);
zset.put("map_key3",10.5);
long zset_result = redisUtils.zset("zset_key",zset,0);
Set<String> zset_value = redisUtils.zrange("zset_key",0,2);
//pipeline
Map<String,String> pipeline = new HashMap<>();
pipeline.put("pipeline_key1","pipeline_value1");
pipeline.put("pipeline_key2","pipeline_value2");
pipeline.put("pipeline_key3","pipeline_value3");
pipeline.put("pipeline_key4","pipeline_value4");
redisUtils.pipeline(pipeline);
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助

12_float_left_people 12_float_left_close