spring-data-redis的使用优化
2016-04-13 16:57
609 查看
1、批量删除keys
2、遍历keys
/** 批量提交 * @param keys * @return */ public long del(final String... keys) throws CacheException { return (long) redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { byte[][] bytes = new byte[keys.length][]; int index = 0; for(String key: keys){ bytes[index++] = stringSerializer.serialize(key); } return connection.del(bytes); } }); } /*循环多次提交(不建议使用) public long del(final String... keys) throws CacheException { return (long) redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { long result = 0; for (int i = 0; i < keys.length; i++) { result += connection.del(redisTemplate.getStringSerializer().serialize(keys[i])); } return result; } }); }*/
2、遍历keys
/** * @param pattern * @return */ public Set<String> keys(final String pattern) throws CacheException { return redisTemplate.execute(new RedisCallback<Set<String>>() { public Set<String> doInRedis(RedisConnection connection) throws DataAccessException { Set<String> keys = new HashSet<String>(); Set<byte[]> set = connection.keys(stringSerializer.serialize(pattern)); for (Iterator<byte[]> iterator = set.iterator(); iterator.hasNext();){ byte[] data = iterator.next(); keys.add(stringSerializer.deserialize(data)); } return keys; } }); } // 注意使用redisTemplate的方法直接调用需要关注序列化规则! Set<byte[]> keys = redisTemplate.keys("*"); // Set<byte[]> keys = redisTemplate.getConnectionFactory().getConnection().keys("*".getBytes(Charset.forName("UTF8")));
相关文章推荐
- 0026-redis服务安装
- Redis学习_01 windows下的环境搭建
- redis学习笔记7--有序集合类型
- Using Redis as Django's session store and cache backend
- redis+mybatis+spring
- redis.conf的配置说明
- Redis主从设置配置
- Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解
- redis入门及其环境搭建
- Windows下安装Redis教程
- redis 集群
- RDB redis
- Linux下Redis开机自启(Centos)
- Redis 字符串处理命令
- Redis keys 基本命令
- 用Redis存储Tomcat集群的Session实现session共享
- Redis 缓存 + Spring 的集成示例
- shiro redis集群介绍
- mybatis缓存 redis实现
- Redis命令-有序集合-zrangebylex