1.redis基础类

package com.qlchat.component.redis.template;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool; import com.qlchat.component.redis.RedisConnectionFactory;
import com.qlchat.component.redis.utils.JsonUtil; public abstract class BasicRedisTemplate { private static Logger logger = LoggerFactory.getLogger(BasicRedisTemplate.class); protected RedisConnectionFactory connectionFactory; public void setConnectionFactory(RedisConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
} protected ShardedJedisPool pool; @PostConstruct
protected void initPool() {
this.pool = connectionFactory.getConnectionPool();
} @SuppressWarnings("deprecation")
protected void closeRedis(ShardedJedis jedis) {
if (jedis != null) {
try {
pool.returnResource(jedis);
} catch (Exception e) {
logger.error("Error happen when return jedis to pool, try to close it directly.", e);
if (jedis != null) {
try {
jedis.disconnect();
} catch (Exception e1) {
}
}
}
}
} /**
* 删除key, 如果key存在返回true, 否则返回false。
*
* @param key
* @return
* @since qlchat 1.0
*/
public boolean del(String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.del(key) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* true if the key exists, otherwise false
*
* @param key
* @return
* @since qlchat 1.0
*/
public Boolean exists(String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.exists(key);
} finally {
this.closeRedis(jedis);
}
} /**
* set key expired time
*
* @param key
* @param seconds
* @return
* @since qlchat 1.0
*/
public Boolean expire(String key, int seconds) {
ShardedJedis jedis = null;
if (seconds == 0) {
return true;
}
try {
jedis = pool.getResource();
return jedis.expire(key, seconds) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
*
* 把object转换为json byte array
*
* @param o
* @return
*/
protected byte[] toJsonByteArray(Object o) {
String json = JsonUtil.toJsonString(o) != null ? JsonUtil.toJsonString(o) : "";
return json.getBytes();
} /**
*
* 把json byte array转换为T类型object
*
* @param b
* @param clazz
* @return
*/
protected <T> T fromJsonByteArray(byte[] b, Class<T> clazz) {
if (b == null || b.length == 0) {
return null;
}
return JsonUtil.parseJson(new String(b), clazz);
} public Long ttl(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.ttl(key);
} finally {
this.closeRedis(jedis);
}
}
}
2.String类型redis工具类 package com.qlchat.component.redis.template; import redis.clients.jedis.ShardedJedis; import com.qlchat.component.redis.RedisException; public class ValueRedisTemplate extends BasicRedisTemplate { /**
* set key-value
*
* @param key
* @param value String
* @throws RedisException
* @since qlchat 1.0
*/
public void set(String key, String value) {
set(key, value, 0);
} /**
* set key-value
*
* @param key
* @param value String
* @throws RedisException
* @since qlchat 1.0
*/
public void set(String key, String value, int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
jedis.set(key, value);
if (ttl > 0) {
jedis.expire(key.getBytes(), ttl);
}
} finally {
this.closeRedis(jedis);
}
} /**
* set key-value For Object(NOT String)
*
* @param key
* @param value Object
* @throws RedisException
* @since qlchat 1.0
*/
public void set(String key, Object value) {
set(key, value, 0);
} /**
* set key-value For Object(NOT String)
*
* @param key
* @param value Object
* @param ttl int
* @throws RedisException
* @since qlchat 1.0
*/
public void set(String key, Object value, int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// jedis.set(key.getBytes(), HessianSerializer.serialize(value));
jedis.set(key.getBytes(), toJsonByteArray(value));
if (ttl > 0) {
jedis.expire(key.getBytes(), ttl);
}
} finally {
this.closeRedis(jedis);
}
} /**
* set key-value with expired time(s)
*
* @param key
* @param seconds
* @param value
* @return
* @throws RedisException
* @since qlchat 1.0
*/
public String setex(String key, int seconds, String value) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.setex(key, seconds, value); } finally {
this.closeRedis(jedis);
}
} /**
* set key-value For Object(NOT String) with expired time(s)
*
* @param key
* @param seconds
* @param value
* @return
* @throws RedisException
* @since qlchat 1.0
*/
public String setex(String key, int seconds, Object value) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return jedis.setex(key.getBytes(), seconds, HessianSerializer.serialize(value));
return jedis.setex(key.getBytes(), seconds, toJsonByteArray(value));
} finally {
this.closeRedis(jedis);
}
} /**
* 如果key还不存在则进行设置,返回true,否则返回false.
*
* @param key
* @param value
* @return
* @throws RedisException
* @since qlchat 1.0
*/
public boolean setnx(String key, String value, int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Long reply = jedis.setnx(key, value);
if(reply == null){
reply = 0L;
}
if (ttl > 0 && reply.longValue() == 1) {
jedis.expire(key, ttl);
}
return reply.longValue() == 1;
} finally {
this.closeRedis(jedis);
}
} /**
* 如果key还不存在则进行设置,返回true,否则返回false.
*
* @param key
* @param value
* @return
* @throws RedisException
* @since qlchat 1.0
*/
public boolean setnx(String key, String value) {
return setnx(key, value, 0);
} /**
* 如果key还不存在则进行设置 For Object,返回true,否则返回false.
*
* @param key
* @param value
* @return
* @throws RedisException
* @since qlchat 1.0
*/
public boolean setnx(String key, Object value) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return jedis.setnx(key.getBytes(), HessianSerializer.serialize(value)) == 1 ? true : false;
return jedis.setnx(key.getBytes(), toJsonByteArray(value)) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* 如果key还不存在则进行设置 For Object,返回true,否则返回false.
*
* @param key
* @param value
* @return
* @throws RedisException
* @since qlchat 1.0
*/
public boolean setnx(String key, Object value, int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// Long reply = jedis.setnx(key.getBytes(), HessianSerializer.serialize(value));
Long reply = jedis.setnx(key.getBytes(), toJsonByteArray(value));
if (ttl > 0) {
jedis.expire(key.getBytes(), ttl);
}
return reply == 1;
} finally {
this.closeRedis(jedis);
}
} /**
* 如果key不存在, 返回null.
*
* @param key
* @return
* @since qlchat 1.0
*/
public String get(String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.get(key);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, 如果key不存在, 返回null.
*
* @param key
* @return
* @since qlchat 1.0
*/
public <T> T get(String key, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return (T) HessianSerializer.deserialize(jedis.get(key.getBytes()));
return fromJsonByteArray(jedis.get(key.getBytes()), clazz);
} finally {
this.closeRedis(jedis);
}
} /**
* 自增 +1
*
* @param key
* @return 返回自增后结果
* @since qlchat 1.0
*/
public Long incr(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.incr(key);
} finally {
this.closeRedis(jedis);
}
} /**
* 自增 +1
*
* @param key key
* @param integer 起始值
* @return 返回自增后结果
* @since qlchat 1.0
*/
public Long incrBy(final String key, long integer) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.incrBy(key, integer);
} finally {
this.closeRedis(jedis);
}
} /**
* 自减 -1
*
* @param key
* @return 返回自减后结果
* @since qlchat 1.0
*/
public Long decr(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.decr(key);
} finally {
this.closeRedis(jedis);
}
} }
Hash类型redis工具类 package com.qlchat.component.redis.template; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import redis.clients.jedis.ShardedJedis; public class HashRedisTemplate extends BasicRedisTemplate { /**
* Set the string value of a hash field
*
* @param key
* @param field
* @param value
* @return
* @since qlchat 1.0
*/
public Boolean hset(final String key, final String field, final String value) {
return hset(key, field, value, 0);
} /**
* Set the string value of a hash field
*
* @param key
* @param field
* @param value
* @param ttl
* @return
* @since qlchat 1.0
*/
public Boolean hset(final String key, final String field, final String value, final int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Long reply = jedis.hset(key, field, value);
if (ttl > 0) {
jedis.expire(key, ttl);
}
return reply == 1;
} finally {
this.closeRedis(jedis);
}
} /**
* Set the Object value of a hash field
*
* @param key
* @param field
* @param value
* @return
* @since qlchat 1.0
*/
public Boolean hset(final String key, final String field, final Object value) {
return hset(key, field, value, 0);
} /**
* Set the Object value of a hash field
*
* @param key
* @param field
* @param value
* @param ttl
* @return
* @since qlchat 1.0
*/
public Boolean hset(final String key, final String field, final Object value, final int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Long reply = jedis.hset(key.getBytes(), field.getBytes(), toJsonByteArray(value));
if (ttl > 0) {
jedis.expire(key, ttl);
}
return reply == 1;
} finally {
this.closeRedis(jedis);
}
} /**
* Get the value of a hash field
*
* @param key
* @param field
* @return
* @since qlchat 1.0
*/
public String hget(final String key, final String field) {
return hget(key, field, 0);
} /**
* Get the value of a hash field
*
* @param key
* @param field
* @return
* @since qlchat 1.0
*/
public String hget(final String key, final String field, int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
String res = jedis.hget(key, field);
if (ttl > 0) {
jedis.expire(key, ttl);
}
return res;
} finally {
this.closeRedis(jedis);
}
} /**
* Get the value of a hash field
*
* @param key
* @param field
* @return
* @since qlchat 1.0
*/
public <T> T hget(final String key, final String field, final Class<T> clazz) {
return hget(key, field, clazz, 0);
} /**
* Get the value of a hash field
*
* @param key
* @param field
* @return
* @since qlchat 1.0
*/
public <T> T hget(final String key, final String field, final Class<T> clazz, final int ttl) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
T res = fromJsonByteArray(jedis.hget(key.getBytes(), field.getBytes()), clazz);
if (ttl > 0) {
this.expire(key, ttl);
}
return res;
} finally {
this.closeRedis(jedis);
}
} /**
* Delete one or more hash fields
*
* @param key
* @param fields
* @return
* @since qlchat 1.0
*/
public Boolean hdel(final String key, final String... fields) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hdel(key, fields) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* Check if a hash field exists
*
* @param key
* @param field
* @return
* @since qlchat 1.0
*/
public Boolean hexists(final String key, final String field) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hexists(key, field);
} finally {
this.closeRedis(jedis);
}
} /**
* Get all the fields and values in a hash
* 当Hash较大时候,慎用!
* @param key
* @return
* @since qlchat 1.0
*/
public Map<String, String> hgetAll(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hgetAll(key);
} finally {
this.closeRedis(jedis);
}
} /**
* Get all the fields and values in a hash
* 当Hash较大时候,慎用!
* @param key
* @return
* @since qlchat 1.0
*/
public Map<String, Object> hgetAllObject(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Map<byte[], byte[]> byteMap = jedis.hgetAll(key.getBytes());
if (byteMap != null && byteMap.size() > 0) {
Map<String, Object> map = new HashMap<String, Object>();
for (Entry<byte[], byte[]> e : byteMap.entrySet()) {
map.put(new String(e.getKey()), fromJsonByteArray(e.getValue(), Object.class));
}
return map;
}
return null;
} finally {
this.closeRedis(jedis);
}
} /**
* Get all the fields and values in a hash
* 当Hash较大时候,慎用!
* @param key
* @return
* @since qlchat 1.0
*/
public <T> Map<String, T> hgetAllObject(final String key, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Map<byte[], byte[]> byteMap = jedis.hgetAll(key.getBytes());
if (byteMap != null && byteMap.size() > 0) {
Map<String, T> map = new HashMap<String, T>();
for (Entry<byte[], byte[]> e : byteMap.entrySet()) {
map.put(new String(e.getKey()), fromJsonByteArray(e.getValue(), clazz));
}
return map;
}
return null;
} finally {
this.closeRedis(jedis);
}
} /**
* Get the values of all the given hash fields.
*
* @param key
* @param fields
* @return
* @since qlchat 1.0
*/
public List<String> hmget(final String key, final String... fields) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hmget(key, fields);
} finally {
this.closeRedis(jedis);
}
} /**
*
* Get the value of a mulit fields
*
* @param key
* @param ttl
* @param fields
* @return
* @since Wifi 1.0
*/
public Map<String, Object> hmgetObject(final String key, final int ttl, final String... fields) {
ShardedJedis jedis = null;
try {
if (null == fields) {
return null;
}
jedis = pool.getResource();
List<byte[]> byteList = new ArrayList<byte[]>();
for (String field : fields) {
byteList.add(field.getBytes());
}
List<byte[]> resBytes = jedis.hmget(key.getBytes(), byteList.toArray(new byte[byteList.size()][]));
Map<String, Object> resMap = null;
if (null != resBytes) {
resMap = new HashMap<String, Object>();
for (int i = 0; i < resBytes.size(); i++) {
resMap.put(fields[i], fromJsonByteArray(resBytes.get(i), Object.class));
}
}
if (ttl > 0) {
this.expire(key, ttl);
}
return resMap;
} finally {
this.closeRedis(jedis);
}
} /**
*
* Get the value of a mulit fields
*
* @param key
* @param ttl
* @param fields
* @return
* @since Wifi 1.0
*/
public Map<String, Object> hmgetObject(final String key, final String... fields) {
return hmgetObject(key, 0, fields);
} /**
* Set multiple hash fields to multiple values.
*
* @param key
* @param hash
* @return
* @since qlchat 1.0
*/
public String hmset(final String key, final Map<String, String> hash) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hmset(key, hash);
} finally {
this.closeRedis(jedis);
}
} /**
* Set multiple hash fields to multiple values.
*
* @param key
* @param hash
* @return
* @since qlchat 1.0
*/
public String hmsetObject(final String key, final Map<String, Object> hash) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Map<byte[], byte[]> byteMap = new HashMap<byte[], byte[]>(hash.size());
for (Entry<String, Object> e : hash.entrySet()) {
byteMap.put(e.getKey().getBytes(), toJsonByteArray(e.getValue()));
}
return jedis.hmset(key.getBytes(), byteMap);
} finally {
this.closeRedis(jedis);
}
} /**
* Increment the integer value of a hash field by the given number.
*
* @param key
* @param field
* @param value
* @return
* @since qlchat 1.0
*/
public Long hincrBy(final String key, final String field, final long value) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hincrBy(key, field, value);
} finally {
this.closeRedis(jedis);
}
} /**
* Get all the fields in a hash.
*
* @param key
* @return
* @since qlchat 1.0
*/
public Set<String> hkeys(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hkeys(key);
} finally {
this.closeRedis(jedis);
}
} /**
* Get all the fields in a hash.
*
* @param key
* @return
* @since qlchat 1.0
*/
public <T> Set<T> hkeys(final String key, final Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<byte[]> set = jedis.hkeys(key.getBytes());
Set<T> objectSet = new HashSet<T>();
if (set != null && set.size() != 0) {
for (byte[] b : set) {
objectSet.add(fromJsonByteArray(b, clazz));
}
}
return objectSet;
} finally {
this.closeRedis(jedis);
}
} /**
* Get the number of fields in a hash.
*
* @param key
* @return
* @since qlchat 1.0
*/
public Long hlen(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hlen(key);
} finally {
this.closeRedis(jedis);
}
} public Long hsetnx(final String key, final String field, final String value) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hsetnx(key, field, value);
} finally {
this.closeRedis(jedis);
} }
}
List基础类型工具类 package com.qlchat.component.redis.template; import java.util.ArrayList;
import java.util.List; import redis.clients.jedis.ShardedJedis; public class ListRedisTemplate extends BasicRedisTemplate { /**
* Prepend one or multiple values to a list
*
* @param key
* @param values
* @since qlchat 1.0
*/
public void lpush(String key, String... values) {
ShardedJedis jedis = null;
if (values == null) {
return;
}
try {
jedis = pool.getResource();
jedis.lpush(key, values);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object,Prepend one or multiple values to a list
*
* @param key
* @param values
* @since qlchat 1.0
*/
public void lpush(String key, Object... values) {
ShardedJedis jedis = null;
if (values == null) {
return;
}
try {
jedis = pool.getResource();
byte[][] strings = new byte[values.length][];
for (int j = 0; j < values.length; j++) {
// strings[j] = HessianSerializer.serialize(values[j]);
strings[j] = toJsonByteArray(values[j]);
}
jedis.lpush(key.getBytes(), strings);
} finally {
this.closeRedis(jedis);
}
} /**
* For single Object,Prepend one or multiple values to a list
*
* @param key
* @param values
* @since qlchat 1.0
*/
public void lpushForObject(String key, Object value) {
ShardedJedis jedis = null;
if (value == null) {
return;
}
try {
jedis = pool.getResource();
// jedis.lpush(key.getBytes(), HessianSerializer.serialize(value));
jedis.lpush(key.getBytes(), toJsonByteArray(value));
} finally {
this.closeRedis(jedis);
}
} /**
* For single Object,Prepend one or multiple values to a list
*
* @param key
* @param values
* @since qlchat 1.0
*/
public void rpushForObject(String key, Object value) {
ShardedJedis jedis = null;
if (value == null) {
return;
}
try {
jedis = pool.getResource();
// jedis.rpush(key.getBytes(), HessianSerializer.serialize(value));
jedis.rpush(key.getBytes(), toJsonByteArray(value));
} finally {
this.closeRedis(jedis);
}
} /**
* Append one or multiple values to a list
*
* @param key
* @param values
* @since qlchat 1.0
*/
public void rpush(String key, String... values) {
ShardedJedis jedis = null;
if (values == null) {
return;
}
try {
jedis = pool.getResource();
jedis.rpush(key, values);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Append one or multiple values to a list
*
* @param key
* @param values
* @since qlchat 1.0
*/
public void rpush(String key, Object... values) {
ShardedJedis jedis = null;
if (values == null) {
return;
}
try {
jedis = pool.getResource();
byte[][] strings = new byte[values.length][];
for (int j = 0; j < values.length; j++) {
// strings[j] = HessianSerializer.serialize(values[j]);
strings[j] = toJsonByteArray(values[j]);
}
jedis.rpush(key.getBytes(), strings);
} finally {
this.closeRedis(jedis);
}
} /**
* Remove and get the last element in a list
*
* @param key
* @return
* @since qlchat 1.0
*/
public String rpop(String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.rpop(key);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Remove and get the last element in a list
*
* @param key
* @param clazz
* @return
* @since qlchat 1.0
*/
public <T> T rpop(String key, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return (T) HessianSerializer.deserialize(jedis.rpop(key.getBytes()));
return fromJsonByteArray(jedis.rpop(key.getBytes()), clazz);
} finally {
this.closeRedis(jedis);
}
} /**
* Remove and get the first element in a list
*
* @param key
* @return
* @since qlchat 1.0
*/
public String lpop(String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.lpop(key);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Remove and get the first element in a list
*
* @param key
* @param clazz
* @return
* @since qlchat 1.0
*/
public <T> T lpop(String key, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return (T) HessianSerializer.deserialize(jedis.lpop(key.getBytes()));
return fromJsonByteArray(jedis.lpop(key.getBytes()), clazz);
} finally {
this.closeRedis(jedis);
}
} /**
* Get the length of a list
*
* @param key
* @return
* @since qlchat 1.0
*/
public Long llen(String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.llen(key);
} finally {
this.closeRedis(jedis);
}
} /**
* 删除List中的等于value的元素
*
* count = 1 :删除第一个; count = 0 :删除所有; count = -1:删除最后一个;
*
* @param key
* @param count
* @param value
* @return
* @since qlchat 1.0
*/
public Long lrem(String key, long count, String value) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.lrem(key, count, value);
} finally {
this.closeRedis(jedis);
}
} /**
* FOR Object, 删除List中的等于value的元素
*
* count = 1 :删除第一个; count = 0 :删除所有; count = -1:删除最后一个;
*
* @param key
* @param count
* @param value
* @return
* @since qlchat 1.0
*/
public Long lrem(String key, long count, Object value) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return jedis.lrem(key.getBytes(), count, HessianSerializer.serialize(value));
return jedis.lrem(key.getBytes(), count, toJsonByteArray(value));
} finally {
this.closeRedis(jedis);
}
} /**
* Get a range of elements from a list.
* <P>
* For example LRANGE foobar 0 2 will return the first three elements of the list.
* </p>
* <P>
* For example LRANGE foobar -1 -2 will return the last two elements of the list.
* </p>
*
* @param key
* @param start
* @param end
* @return
* @since qlchat 1.0
*/
public List<String> lrange(String key, long start, long end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.lrange(key, start, end);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Get a range of elements from a list.
* <P>
* For example LRANGE foobar 0 2 will return the first three elements of the list.
* </p>
* <P>
* For example LRANGE foobar -1 -2 will return the last two elements of the list.
* </p>
*
* @param key
* @param start
* @param end
* @return
* @since qlchat 1.0
*/
public <T> List<T> lrange(String key, long start, long end, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
List<byte[]> list = jedis.lrange(key.getBytes(), start, end);
if (list != null && list.size() > 0) {
List<T> results = new ArrayList<>();
for (byte[] bytes : list) {
results.add(fromJsonByteArray(bytes, clazz));
}
return results;
}
return null;
} finally {
this.closeRedis(jedis);
}
} public String ltrim(String key, long start, long end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.ltrim(key, start, end);
} finally {
this.closeRedis(jedis);
}
}
}
set基础类型工具类 package com.qlchat.component.redis.template; import java.util.List;
import java.util.Set;
import java.util.TreeSet; import redis.clients.jedis.ShardedJedis; /**
*
* <b><code>SetRedisTemplate</code></b>
* <p>
* Set 数据结构的常用方法封装,支持Object 类型
* </p>
* <b>Creation Time:</b> 2016年10月12日 下午5:51:46
*
*/
public class SetRedisTemplate extends BasicRedisTemplate { /**
* Add one or more members to a set
*
* @param key
* @param members
* @return
* @since qlchat 1.0
*/
public Boolean sadd(final String key, final String... members) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.sadd(key, members) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* Add one or more members to a set
*
* @param key
* @param ttl
* @param members
* @return
* @since qlchat 1.0
*/
public Boolean sadd(final String key,int ttl, final String... members) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Boolean ret = jedis.sadd(key, members) == 1 ? true : false;
if (ret && ttl > 0) {
jedis.expire(key, ttl);
}
return ret;
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Add one or more members to a set
*
* @param key
* @param members
* @return
* @since qlchat 1.0
*/
public Boolean sadd(final String key, final Object... members) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
byte[][] strings = new byte[members.length][];
for (int j = 0; j < members.length; j++) {
strings[j] = toJsonByteArray(members[j]);
}
return jedis.sadd(key.getBytes(), strings) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* Remove one or more members from a set
*
* @param key
* @param members
* @return
* @since qlchat 1.0
*/
public Boolean srem(final String key, final String... members) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.srem(key, members) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Remove one or more members from a set
*
* @param key
* @param members
* @return
* @since qlchat 1.0
*/
public Boolean srem(final String key, final Object... members) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
byte[][] strings = new byte[members.length][];
for (int j = 0; j < members.length; j++) {
strings[j] = toJsonByteArray(members[j]);
}
return jedis.srem(key.getBytes(), strings) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* Get all the members in a set.
*
* @param key
* @return
* @since qlchat 1.0
*/
public Set<String> smembers(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.smembers(key);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Get all the members in a set.
*
* @param key
* @param clazz
* @return
* @since qlchat 1.0
*/
public <T> Set<T> smembers(final String key, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<byte[]> tempSet = jedis.smembers(key.getBytes());
if (tempSet != null && tempSet.size() > 0) {
TreeSet<T> result = new TreeSet<T>();
for (byte[] value : tempSet) {
result.add(fromJsonByteArray(value, clazz));
}
return result;
}
return null;
} finally {
this.closeRedis(jedis);
}
} /**
* Get all the members is number in a set.
*
* @param key
* @return
* @since qlchat 1.0
*/
public Long scard(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.scard(key);
} finally {
this.closeRedis(jedis);
}
} /**
* true if the meber exists in a set,else false
*
* @param key
* @param member
* @return
* @since qlchat 1.0
*/
public Boolean sismember(final String key, final String member) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.sismember(key, member);
} finally {
this.closeRedis(jedis);
}
} public String spop(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.spop(key);
} finally {
this.closeRedis(jedis);
}
} public List<String> srandmember(final String key,final int count) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.srandmember(key, count);
} finally {
this.closeRedis(jedis);
}
} }
Zset工具类 package com.qlchat.component.redis.template; import com.alibaba.fastjson.JSON;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.Tuple; import java.util.*; /**
*
* <b><code>ZSetRedisTemplate</code></b>
* <p>
* Sorted Sets 数据结构的常用方法封装,支持Object 类型
* </p>
* <b>Creation Time:</b> 2016年10月12日 下午5:54:09
* @author abin.yao
* @since qlchat 1.0
*/
public class ZSetRedisTemplate extends BasicRedisTemplate { /**
* 加入Sorted set, 如果member在Set里已存在, 只更新score并返回false, 否则返回true.
*
* @param key
* @param member
* @param score
* @return
* @since qlchat 1.0
*/
public Boolean zadd(String key, double score, String member) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zadd(key, score, member) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* 加入Sorted set, 如果member在Set里已存在, 只更新score并返回false, 否则返回true.
*
* @param key
* @param member
* @param score
* @param ttl 过期时间,秒
* @return
* @since qlchat 1.0
*/
public Boolean zadd(final String key,final double score,final int ttl,final String member) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Boolean ret = jedis.zadd(key, score, member) == 1 ? true : false;
if (ret && ttl > 0) {
jedis.expire(key, ttl);
}
return ret;
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, 加入Sorted set, 如果member在Set里已存在, 只更新score并返回false, 否则返回true.
*
* @param key
* @param member
* @param score
* @return
* @since qlchat 1.0
*/
public Boolean zadd(String key, double score, Object member) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return jedis.zadd(key.getBytes(), score, HessianSerializer.serialize(member)) == 1 ? true : false;
return jedis.zadd(key.getBytes(), score, toJsonByteArray(member)) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* Return a range of members in a sorted set, by index. Ordered from the lowest to the highest score.
*
* @param key
* @param start
* @param end
* @return Ordered from the lowest to the highest score.
* @since qlchat 1.0
*/
public Set<String> zrange(String key, long start, long end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrange(key, start, end);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Return a range of members in a sorted set, by index.Ordered from the lowest to the highest score.
*
* @param key
* @param start
* @param end
* @return Ordered from the lowest to the highest score.
* @since qlchat 1.0
*/
public <T> List<T> zrange(String key, long start, long end, Class<T> clazz) {
List<T> result = new ArrayList<>();
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<byte[]> tempSet = jedis.zrange(key.getBytes(), start, end);
if (tempSet != null && tempSet.size() > 0) {
for (byte[] value : tempSet) {
// result.add((T) HessianSerializer.deserialize(value));
result.add(fromJsonByteArray(value, clazz));
}
return result;
}
return null;
} finally {
this.closeRedis(jedis);
}
} /**
* Return a range of members in a sorted set, by index. Ordered from the highest to the lowest score.
*
* @param key
* @param start
* @param end
* @return Ordered from the highest to the lowest score.
* @since qlchat 1.0
*/
public Set<String> zrevrange(String key, long start, long end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrevrange(key, start, end);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Return a range of members in a sorted set, by index. Ordered from the highest to the lowest score.
*
* @param key
* @param start
* @param end
* @param clazz
* @return Ordered from the highest to the lowest score.
* @since qlchat 1.0
*/
public <T> List<T> zrevrange(String key, long start, long end, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<byte[]> tempSet = jedis.zrevrange(key.getBytes(), start, end);
if (tempSet != null && tempSet.size() > 0) {
List<T> result = new ArrayList<>();
for (byte[] value : tempSet) {
// result.add((T) HessianSerializer.deserialize(value));
result.add(fromJsonByteArray(value, clazz));
}
return result;
}
return null;
} finally {
this.closeRedis(jedis);
}
} /**
* Return the all the elements in the sorted set at key with a score between
* min and max (including elements with score equal to min or max).
*
* @param key
* @param min
* @param max
* @return Ordered from the lowest to the highest score.
* @since qlchat 1.0
*/
public Set<String> zrangeByScore(final String key, final double min, final double max) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrangeByScore(key, min, max);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Return the all the elements in the sorted set at key with a score between
* min and max (including elements with score equal to min or max).
*
* @param key
* @param min
* @param max
* @return Ordered from the lowest to the highest score.
* @since qlchat 1.0
*/
public <T> Set<T> zrangeHashSetByScore(final String key, final double min, final double max, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<byte[]> tempSet = jedis.zrangeByScore(key.getBytes(), min, max);
if (tempSet != null && tempSet.size() > 0) {
HashSet<T> result = new HashSet<T>();
for (byte[] value : tempSet) {
// result.add((T) HessianSerializer.deserialize(value));
result.add(fromJsonByteArray(value, clazz));
}
return result;
}
return null;
} finally {
this.closeRedis(jedis);
}
} /**
* Return the all the elements in the sorted set at key with a score between
* min and max (including elements with score equal to min or max).
* @param key
* @param min
* @param max
* @return Ordered from the highest to the lowest score.
* @since qlchat 1.0
*/
public Set<String> zrevrangeByScore(final String key, final double min, final double max) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrevrangeByScore(key, max, min);
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Return the all the elements in the sorted set at key with a score between
* min and max (including elements with score equal to min or max).
*
* @param key
* @param min
* @param max
* @return Ordered from the lowest to the highest score.
* @since qlchat 1.0
*/
public <T> List<T> zrangeByScore(final String key, final double min, final double max, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<byte[]> tempSet = jedis.zrangeByScore(key.getBytes(), min, max);
if (tempSet != null && tempSet.size() > 0) {
List<T> result = new ArrayList<>();
for (byte[] value : tempSet) {
result.add(fromJsonByteArray(value, clazz));
}
return result;
}
return null;
} finally {
this.closeRedis(jedis);
}
} public <T> List<T> zrangeByScore(final String key, final double min, final double max, final int offset, final int count, Class<T> clazz) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<byte[]> tempSet = jedis.zrangeByScore(key.getBytes(), min, max, offset, count);
if (tempSet != null && tempSet.size() > 0) {
List<T> result = new ArrayList<>();
for (byte[] value : tempSet) {
result.add(fromJsonByteArray(value, clazz));
}
return result;
}
return null;
} finally {
this.closeRedis(jedis);
}
} /**
* Return a range of members with scores in a sorted set, by index. Ordered from the lowest to the highest score.
*
* @param key
* @param start
* @param end
* @return
* @since qlchat 1.0
*/
public Set<Tuple> zrangeWithScores(final String key, final long start, final long end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrangeWithScores(key, start, end);
} finally {
this.closeRedis(jedis);
}
} /**
* Return a range of members with scores in a sorted set, by index. Ordered from the highest to the lowest score.
*
* @param key
* @param start
* @param end
* @return
* @since qlchat 1.0
*/
public Set<Tuple> zrevrangeWithScores(final String key, final long start, final long end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrevrangeWithScores(key, start, end);
} finally {
this.closeRedis(jedis);
}
} /**
* Return the all the elements in the sorted set at key with a score between
* min and max (including elements with score equal to min or max). Ordered from the lowest to the highest score.
*
* @param key
* @param min
* @param max
* @return Ordered from the lowest to the highest score.
* @since qlchat 1.0
*/
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrangeByScoreWithScores(key, min, max);
} finally {
this.closeRedis(jedis);
}
} public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrangeByScoreWithScores(key, min, max, offset, count);
} finally {
this.closeRedis(jedis);
}
} /**
* Return the all the elements in the sorted set at key with a score between
* min and max (including elements with score equal to min or max). Ordered from the highest to the lowest score.
*
* @param key
* @param min
* @param max
* @return Ordered from the highest to the lowest score.
* @since qlchat 1.0
*/
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double min, final double max) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrevrangeByScoreWithScores(key, max, min);
} finally {
this.closeRedis(jedis);
}
} public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrevrangeByScoreWithScores(key, max, min, offset, count);
} finally {
this.closeRedis(jedis);
}
} /**
* Remove one or more members from a sorted set
*
* @param key
* @param members
* @return
* @since qlchat 1.0
*/
public Boolean zrem(final String key, final String... members) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrem(key, members) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* For Object, Remove one or more members from a sorted set
*
* @param key
* @param members
* @return
* @since qlchat 1.0
*/
public Boolean zrem(final String key, final Object... members) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
byte[][] strings = new byte[members.length][];
for (int j = 0; j < members.length; j++) {
// strings[j] = HessianSerializer.serialize(members[j]);
strings[j] = toJsonByteArray(members[j]);
}
return jedis.zrem(key.getBytes(), strings) == 1 ? true : false;
} finally {
this.closeRedis(jedis);
}
} /**
* Get the score associated with the given member in a sorted set
*
* @param key
* @param member
* @return
* @since qlchat 1.0
*/
public Double zscore(final String key, final String member) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zscore(key, member);
} finally {
this.closeRedis(jedis);
}
} /**
* For ObjecGet the score associated with the given member in a sorted set
*
* @param key
* @param member
* @return
* @since qlchat 1.0
*/
public Double zscore(final String key, final Object member) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
// return jedis.zscore(key.getBytes(), HessianSerializer.serialize(member));
return jedis.zscore(key.getBytes(), toJsonByteArray(member));
} finally {
this.closeRedis(jedis);
}
} /**
* Remove all elements in the sorted set at key with rank between start and
* end. Start and end are 0-based with rank 0 being the element with the
* lowest score. Both start and end can be negative numbers, where they
* indicate offsets starting at the element with the highest rank. For
* example: -1 is the element with the highest score, -2 the element with
* the second highest score and so forth.
* <p>
* <b>Time complexity:</b> O(log(N))+O(M) with N being the number of
* elements in the sorted set and M the number of elements removed by the
* operation
* @param key
* @param start
* @param end
* @return
* @since qlchat 1.0
*/
public Long zremrangeByRank(String key, long start, long end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zremrangeByRank(key, start, end);
} finally {
this.closeRedis(jedis);
}
} /**
*
* Get the length of a sorted set
*
* @param key
* @param min
* @param max
* @return
* @since qlchat 1.0
*/
public Long zcount(final String key, final double min, final double max) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zcount(key, min, max);
} finally {
this.closeRedis(jedis);
}
} /**
* Get the number of members in a sorted set
* @param key key
* @return Long
*/
public Long zcard(final String key) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zcard(key);
} finally {
this.closeRedis(jedis);
}
} public <T> List<T> zrevrangeByScore(String key, double max, double min, Class<T> clazz) {
List<T> result = new ArrayList<T>();
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<String> sets = jedis.zrevrangeByScore(key, max, min);
if (null == sets || sets.size() == 0) {
return result;
}
for (String s : sets) {
result.add(JSON.parseObject(s, clazz));
}
} catch (Exception e) {
e.printStackTrace(); } finally {
if(jedis != null) {jedis.close();}
}
return result;
} public <T> List<T> zrevrangeByScore(String key, double max, double min, int offset, int count, Class<T> clazz) {
List<T> result = new ArrayList<T>();
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<String> sets = jedis.zrevrangeByScore(key, max, min, offset, count);
if (null == sets || sets.size() == 0) {
return result;
}
for (String s : sets) {
result.add(JSON.parseObject(s, clazz));
}
} catch (Exception e) {
e.printStackTrace(); } finally {
if(jedis != null) {jedis.close();}
}
return result;
} public List<String> zrevrangeByScore(String key, double max, double min, int offset, int count) {
List<String> result = new ArrayList<>();
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<String> sets = jedis.zrevrangeByScore(key, max, min, offset, count);
if (null == sets || sets.size() == 0) {
return result;
}
for (String s : sets) {
result.add(s);
}
} catch (Exception e) {
e.printStackTrace(); } finally {
if(jedis != null) {jedis.close();}
}
return result;
} public Set<String> zrangeByScore(String key, double max, double min, int offset, int count) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zrangeByScore(key, min, max, offset, count);
} finally {
this.closeRedis(jedis);
}
} public Long zremrangeByScore(String key, double start, double end) {
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
return jedis.zremrangeByScore(key, start, end);
}finally {
this.closeRedis(jedis);
}
} public Map<String,Object> zrevrankWithScore(String key,String element){
ShardedJedis jedis = null;
Map<String,Object> map = new HashMap<>();
try{
Long rankIndex = jedis.zrevrank(key,element);
Double score = jedis.zscore(key,element);
if(rankIndex != null && score >0){
map.put("rank",rankIndex+1);
map.put("score",score);
}
}catch (Exception e){
e.printStackTrace();
}finally {
this.closeRedis(jedis);
}
return map;
}
}
HyperLogLog模板 package com.qlchat.component.redis.template; import redis.clients.jedis.ShardedJedis; /**
* HyperLogLog模板
* Created by liujunjie on 16-12-15.
*/
public class HyperLogLogTemplate extends BasicRedisTemplate { /**
* 添加
* @param key key
* @param elements 元素
*/
public Long pfadd(String key, String... elements) {
ShardedJedis jedis = null;
try{
jedis = pool.getResource();
return jedis.pfadd(key, elements);
} finally {
this.closeRedis(jedis);
}
} /**
* 统计
* @param key key
* @return long
*/
public long pfcount(String key) {
ShardedJedis jedis = null;
try{
jedis = pool.getResource();
return jedis.pfcount(key);
} finally {
this.closeRedis(jedis);
}
} }

java中的redis工具类的更多相关文章

  1. Redis 工具类 java 实现的redis 工具类

    最近了解了一下非关系型数据库 redis 会使用简单的命令 在自己本地电脑 使用时必须先启动服务器端 在启动客户端 redis 简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...

  2. JAVA中封装JSONUtils工具类及使用

    在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...

  3. Java 中的并发工具类

    Java 中的并发工具类 CountDownLatch public class JoinCountDownLatchTest { public static void main(String[] a ...

  4. Redis在JAVA中的运用(工具类)

    最近项目需要用redis在中间做缓存所以写了一个工具类作为练习用 redis版本:redis_version:3.0.504 用到阿里的解析JSON的库:fastjson import org.apa ...

  5. java中常用的工具类(一)

    我们java程序员在开发项目的是常常会用到一些工具类.今天我汇总了一下java中常用的工具方法.大家可以在项目中使用.可以收藏!加入IT江湖官方群:383126909 我们一起成长 一.String工 ...

  6. java中常用的工具类(三)

    继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类       ...

  7. java中常用的工具类(二)

    下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil           Java   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  8. Java中的并发工具类(CountDownLatch、CyclicBarrier、Semaphore、Exchanger)

    在JDK的并发包里提供了很多有意思的并发工具类.CountDownLatch.CyclicBarrier和Semaphore 工具类提供了一种并发流程控制的手段,Exchanger 工具类则提供了在线 ...

  9. 在JAVA中封装JSONUtil工具类及使用

    在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...

随机推荐

  1. WebSocket握手总结

    网址:http://blog.csdn.net/edwingu/article/details/44040961 WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器 ...

  2. bzoj 1601 灌水

    题目大意: 决定把水灌到n块农田,农田被数字1到n标记 把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库 建造一个水库需要花费wi,连接两块土地需要花费Pij. 计算所需的最少代价 ...

  3. P2597 [ZJOI2012]灾难 拓扑排序

    这个题有点意思,正常写法肯定会T,然后需要优化.先用拓扑排序重构一遍树,然后进行一个非常神奇的操作:把每个点放在他的食物的lca上,然后计算的时候直接dfs全加上就行了.为什么呢,因为假如你的食物的l ...

  4. Black Rock Shooter

    在人气动漫 Black Rock shooter 中,当加贺里对麻陶 说出了"滚回去"以后,与此同时,在另一个心灵世界里, BRS 也遭到了敌人的攻击.此时,一共有 n 个攻击排成 ...

  5. Euclid(几何)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2831 题意:已知A,B,C,D,E,F的坐标, ...

  6. php 时间戳和时间的转换

    PHP的时间戳与具体时间转化 三个内置函数: time() //获取UNIX系统时间戳 mktime(hour,minute,second,month,day,year) //将指定时间转化为时间戳 ...

  7. 一款超好用的第三方评论插件--Gitalk

    一,使用Gitalk的背景: 1.最近在做一个基于Java的个人博客系统,已经基本完工了,突然发现怎么没有评论的操作,如果再从头开始从数据库开始写的话,花费的代价有点大,于是乎我就在网上寻找一款适合我 ...

  8. OI——不后悔的两年

    NOI2014,悲惨的考跪,99+170+130 399 Cu滚粗.最终签到了复旦的一本,还算是有点结果吧.(其实我一开始就想读复旦我会说?)回首这两年,就像一场梦一样,从一无所知的小白到进入省队再到 ...

  9. Wannafly挑战赛19 A-队列Q

    题目描述 ZZT 创造了一个队列 Q.这个队列包含了 N 个元素,队列中的第 i 个元素用 Qi 表示.Q1 表示队头元素,QN 表示队尾元素.队列中的元素是 N 的一个全排列. ZZT 需要在这个队 ...

  10. 简单入门构建spark1.6.1源码环境

    能有源码的辅助,加上自身的修炼,能起到很好的作用! 对于初学者,不建议,一上来看源码. 下载 http://archive.apache.org/dist/spark/spark-1.6.1/