标签:
Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案。
Redis从它的许多竞争继承来的三个主要特点:
Redis数据库完全在内存中,使用磁盘仅用于持久性。
相比许多键值数据存储,Redis拥有一套较为丰富的数据类型。
Redis可以将数据复制到任意数量的从服务器。
Redis 优势如下:
异常快速:Redis的速度非常快,每秒能执行约11万集合,每秒约81000+条记录。
支持丰富的数据类型:Redis支持最大多数开发人员已经知道像列表,集合,有序集合,散列数据类型。这使得它非常容易解决各种各样的问题,因为我们知道哪些问题是可以处理通过它的数据类型更好。
操作都是原子性:所有Redis操作是原子的,这保证了如果两个客户端同时访问的Redis服务器将获得更新后的值。
多功能实用工具:Redis是一个多实用的工具,可以在多个用例如缓存,消息,队列使用(Redis原生支持发布/订阅),任何短暂的数据,应用程序,如Web应用程序会话,网页命中计数等。
spring-data-redis 封装 RedisService 操作封装源码如下:
package com.yoodb.service; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.Resource; import org.apache.shiro.dao.DataAccessException; import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisListCommands.Position; import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; import org.springframework.data.redis.connection.SortParameters; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Service; /** * @author yoodb * @出自 素文宅 * @url www.yoodb.com * @注意 转载请注明www.yoodb.com * @param <K> * @param <V> */ @Service public class CopyOfRedisService<K, V> { @Resource(name = "redisTemplate") protected RedisTemplate<K, V> redisTemplate; /** * 设置key */ public Boolean set(final String key, final String value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); byte[] values = serializer.serialize(value); connection.set(keys, values); return true; } }); } return false; } /** * 根据key获取对象 */ public String get(final String key) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<String>() { public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); byte[] values = connection.get(keys); if (values == null) { return null; } String value = serializer.deserialize(values); return value; } }); } return null; } /** * 根据key删除 * @param key * @return */ public Long del(final String key) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.del(keys); } }); } return null; } /** * 某段时间后执行 * @param key * @param value * @return */ public Boolean expire(final String key, final long value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.expire(keys, value); } }); } return false; } /** * 在某个时间点失效 * @param key * @param value * @return */ public Boolean expireAt(final String key, final long value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.expireAt(keys, value); } }); } return false; } /** * 查询剩余时间 * @param key * @param value * @return */ public Long ttl(final String key, final long value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.ttl(keys); } }); } return 0l; } /** * 判断key是否存在 * @param key * @return */ public Boolean exists(final String key) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.exists(keys); } }); } return false; } /** * 返回 key 所储存的值的类型 * @param key * @return */ public DataType type(final String key) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<DataType>() { public DataType doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.type(keys); } }); } return null; } /** * 对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit) * @param key * @param offset * @param value * @return */ public Boolean setBit(final String key,final long offset,final boolean value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); connection.setBit(keys,offset,value); return true; } }); } return false; } /** * 对 key 所储存的字符串值,获取指定偏移量上的位(bit) * @param key * @param value * @return */ public Boolean getBit(final String key ,final long value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.getBit(keys, value); } }); } return false; } /** * 用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始 * @param key * @param offset * @param value * @return */ public Boolean setRange(final String key,final Long offset,final String value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); byte[] values = serializer.serialize(value); connection.setRange(keys,values,offset); return true; } }); } return false; } /** * 返回 key 中字符串值的子字符串,字符串的截取范围由 start 和 end 两个偏移量决定 * @param key * @param startOffset * @param endOffset * @return */ public byte[] getRange(final String key,final long startOffset,final long endOffset) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<byte[]>() { public byte[] doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); return connection.getRange(keys,startOffset,endOffset); } }); } return null; } /** * 删除对象 ,依赖key */ public void delete(String key) { List<String> list = new ArrayList<String>(); list.add(key); delete(list); } /** * 删除集合 ,依赖key集合 */ @SuppressWarnings("unchecked") private void delete(List<String> keys) { redisTemplate.delete((K) keys); } /** * 根据参数 count 的值,移除列表中与参数 value 相等的元素 * @param keyStr * @param count * @param valueStr * @return */ public Long lrem(final String key, final long count, final String value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); byte[] values = serializer.serialize(value); return connection.lRem(keys, count, values); } }); } return null; } /** * 将一个或多个值 value 插入到列表 key 的表头 * @param keyStr * @param valueStr * @return */ public Long lpush(final String key, final String value) { if (redisTemplate != null) { redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); byte[] values = serializer.serialize(value); return connection.lPush(keys, values); } }); } return null; } .....内容不全...... 注意 全部源码地址 http://www.yoodb.com/article/display/1065
标签:
原文地址:http://my.oschina.net/freelife/blog/517127