- 浏览: 52946 次
- 性别:
文章分类
最新评论
Spring-data-redis使用
1、maven依赖
2、spring配置
3、redis引用代码:
1、maven依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${redis.client.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${spring.data.redis}</version> </dependency>
2、spring配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="10000" /> <property name="maxIdle" value="100" /> <property name="maxWaitMillis" value="10000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="connectionFactory"> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="connectionFactory"> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean> </beans>
3、redis引用代码:
import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import com.ppfuns.video.core.util.ExceptionEnum; import com.ppfuns.video.core.util.ExceptionGlobal; import com.ppfuns.video.core.util.MessageUtil; @Service public class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; private Lock lock = new ReentrantLock(); //******************************************String Ops ********************************************** /** * * @Title: set * @Description: 添加数据 * @param key * @param value * @return void * @throws */ public void set(final String key, final String value) { this.redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { connection.set(key.getBytes(), value.getBytes()); return null; } }); } /** * * @Title: append * @Description: 追加数据 * @param key * @param value * @return * @return long * @throws */ public long append(final String key, final String value) { long sum = this.redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { return connection.append(key.getBytes(), value.getBytes()); } }); return sum; } /** * * @Title: set * @Description: 添加带有过期时间的数据 * @param key * @param value * @param seconds * @return void * @throws */ public void set(final String key, final String value, final long seconds){ this.redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(key.getBytes(), seconds, value.getBytes()); return null; } }); } /*** * * @Title: del * @Description: 删除key下数据 * @param key * @return * @return long * @throws */ public long del(final String key) { long s = this.redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { long sum = connection.del(key.getBytes()); return sum; } }); return s; } /** * * @Title: del * @Description: 删除数据keys数据 * @param keys * @return 返回删除数据size * @return long * @throws */ public long del(final String... keys){ long s = this.redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { long sum = 0; for (String key : keys) { sum = sum + connection.del(key.getBytes()); } return sum; } }); return s; } /** * * @Title: get * @Description: 获取数据 * @param key * @return * @return String * @throws */ public String get(final String key){ String value = this.redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { if (connection.exists(key.getBytes())) { byte[] values = connection.get(key.getBytes()); return redisTemplate.getStringSerializer().deserialize(values); } return null; } }); return value; } /** * * @Title: size * @Description: 获取key下size * @param key * @return * @return long * @throws */ public long size(final String key){ long s = this.redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { long sum = connection.sCard(key.getBytes()); return sum; } }); return s; } /** * * @Title: exist * @Description: 是否存在key * @param key * @return true/false * @return boolean * @throws */ public boolean exist(final String key){ boolean b = this.redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { boolean bool = connection.exists(key.getBytes()); return bool; } }); return b; } /** * * @Title: keys * @Description: 获取keys * @param pattern * @return * @return Set<byte[]> * @throws */ public List<String> keys(final String pattern){ return this.redisTemplate.execute(new RedisCallback<List<String>>() { @Override public List<String> doInRedis(RedisConnection connection) throws DataAccessException { Set<byte[]> set = connection.keys(pattern.getBytes()); List<String> list = new ArrayList<String>(); for (byte[] b : set) { list.add(new String(b)); } return list; } }); } //******************************************List Ops************************************************** /** * * @Title: listSetR * @Description: 左添加数据 * @param key * @param value * @return 添加数据size * @return long * @throws */ public long listSetR(final String key, final String value){ return this.redisTemplate.boundListOps(key).rightPush(value); } /** * * @Title: listSetR * @Description: 添加带有过期时间的数据 * @param key * @param value * @param seconds * @return * @return long * @throws */ public long listSetR(final String key, final String value, final long seconds){ long sum = this.redisTemplate.boundListOps(key).rightPush(value); boolean bool = this.redisTemplate.boundListOps(key).expire(seconds, TimeUnit.SECONDS); if (bool) { return sum; } return 0; } /** * * @Title: listSetR * @Description: 批量添加数据 * @param key * @param values * @return * @return long * @throws InterruptedException */ public long listSetR(final String key, final String... values) { try { lock.lockInterruptibly(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { return this.redisTemplate.opsForList().rightPushAll(key, values); } finally { lock.unlock(); } } /** * * @Title: listSetR * @Description: 带有过期时间的批量数据添加 * @param key * @param seconds * @param values * @return * @throws InterruptedException * @return long * @throws */ public long listSetR(final String key, final long seconds, final String... values) { try { lock.lockInterruptibly(); long sum = 0; try { sum = this.redisTemplate.boundListOps(key).rightPushAll(values); boolean bool = this.redisTemplate.boundListOps(key).expire(seconds, TimeUnit.SECONDS); if (bool) { return sum; } } finally { lock.unlock(); } } catch (InterruptedException e) { MessageUtil.sendMessage(ExceptionEnum.ERROR, "BlueRayCore.RedisService.listSetR", ExceptionGlobal.getExceptionInfo(e)); } return 0; } /** * * @Title: listDel * @Description: 删除数据 * @param key * @return * @return boolean * @throws */ public boolean listDel(final String key){ this.redisTemplate.boundListOps(key).expire(1, TimeUnit.MILLISECONDS); try { Thread.sleep(2); } catch (InterruptedException e) { MessageUtil.sendMessage(ExceptionEnum.ERROR, "BlueRayCore.RedisService.listDel", ExceptionGlobal.getExceptionInfo(e)); } finally { } return this.redisTemplate.boundListOps(key).persist(); } /** * * @Title: listGet * @Description: 获取集合数据 * @param key * @return * @return String * @throws */ public String listGet(final String key){ return this.redisTemplate.boundListOps(key).leftPop(); } /** * * @Title: listSize * @Description: 获取集合大小 * @param key * @return * @return long * @throws */ public long listSize(final String key){ return this.redisTemplate.boundListOps(key).size(); } }
发表评论
-
java 之sftp实现
2018-03-31 17:41 658上周进行了linux环境下sftp的配置和用户权限的创建:ht ... -
java webService之CXF的使用
2018-03-29 14:25 448使用场景:华为VOD系统与媒资系统的接口,资产数据(元数据XM ... -
java 中jstat的用法
2018-03-20 18:06 719JDK自带VM分析工具jps,jstat,jmap,jcons ... -
javaEE性能优化
2018-03-20 16:41 504性能的优化一般可以从多方面入手,前端资源,java程序,数据传 ... -
jvm gc日志检查
2018-03-20 15:08 368JVM的GC日志的主要参数包括如下几个: -XX:+Pr ... -
负载均衡的几种原理
2018-03-19 16:05 472什么是负载均衡? 就 ... -
基于TCP协议实现RPC
2018-03-19 11:28 714RPC的全称:Remote Process Call,即远程过 ... -
java 多线程
2018-03-16 13:29 356Java 多线程编程 Java 给 ... -
SpringMVC执行流程图
2018-03-15 10:46 471SpringMVC 流程图 DispatcherServle ... -
Java中BIO、NIO、AIO的原理及其区别
2018-03-12 17:34 661IO的处理方式通常分为 ... -
基于Spring 自定义标签实现
2017-11-30 09:26 556一、源码分析: Spring标签的定义分为默认标签和自定义 ... -
java二维码的生成和解析
2017-09-26 11:15 434一、本文目的: 为了研究对支付宝和微信支付的统一路口管理 ... -
java,redis
2017-09-18 11:32 358Redis 简介 Redis 是完全开源免费的,遵守BSD协议 ... -
Java xml与实体Bean的转换
2017-09-05 15:24 7001、pom.xml依赖包: <dependenc ... -
Java JVM虚拟机知识要点
2017-08-30 10:25 5961、JVM虚拟机图解: ... -
java 上传小于占用空间为4k的jpg图片异常问题处理
2017-07-19 18:02 529javaWeb上传图片 jpg占用空间为4k时,Commons ... -
Springmvc 注入字符串与时间格式的转换
2017-03-24 11:10 1196以下列出两种spring支持的时间转换 -、方式一 1、 ... -
linux下修改war包
2017-03-06 15:32 1101Linux上修改war包上的文件 www.MyExceptio ... -
itellij idea 11.1.3 mybatis 自动构建代码
2016-11-16 09:50 445步骤一: 1、在工程中pom.xml加添 <buil ... -
intellij idea 下resin容器远程调试
2016-11-16 09:27 5386resin远程调试(我使用的是resin-4.0.41版本): ...
相关推荐
赠送jar包:spring-data-redis-2.6.1.jar; 赠送原API文档:spring-data-redis-2.6.1-javadoc.jar; 赠送源代码:spring-data-redis-2.6.1-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.6.1.pom; ...
赠送jar包:spring-data-redis-2.3.9.RELEASE.jar; 赠送原API文档:spring-data-redis-2.3.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.3.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...
赠送jar包:spring-data-redis-2.5.5.jar; 赠送原API文档:spring-data-redis-2.5.5-javadoc.jar; 赠送源代码:spring-data-redis-2.5.5-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.5.5.pom; ...
赠送jar包:spring-data-redis-2.0.9.RELEASE.jar; 赠送原API文档:spring-data-redis-2.0.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.0.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...
赠送jar包:spring-data-redis-2.5.5.jar; 赠送原API文档:spring-data-redis-2.5.5-javadoc.jar; 赠送源代码:spring-data-redis-2.5.5-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.5.5.pom; ...
赠送jar包:spring-data-redis-2.0.6.RELEASE.jar; 赠送原API文档:spring-data-redis-2.0.6.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.0.6.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...
赠送jar包:spring-data-redis-1.7.5.RELEASE.jar 赠送原API文档:spring-data-redis-1.7.5.RELEASE-javadoc.jar 赠送源代码:spring-data-redis-1.7.5.RELEASE-sources.jar 包含翻译后的API文档:spring-data-...
赠送jar包:spring-data-redis-2.0.9.RELEASE.jar; 赠送原API文档:spring-data-redis-2.0.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.0.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...
赠送jar包:spring-data-redis-2.6.1.jar; 赠送原API文档:spring-data-redis-2.6.1-javadoc.jar; 赠送源代码:spring-data-redis-2.6.1-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.6.1.pom; ...
赠送jar包:spring-data-redis-1.7.5.RELEASE.jar; 赠送原API文档:spring-data-redis-1.7.5.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-1.7.5.RELEASE-sources.jar; 包含翻译后的API文档:spring-...
spring-data-redis-1.8.1.RELEASE-sources.jar(spring-data-redis-1.8.1.RELEASE-sources.jar()
jedis、spring-redis-datade的整合使用,如果版本不匹配可能存在不兼容的问题,从而产生异常。 这里给出无异常的版本匹配: 1、spring-data-redis-1.7.2.RELEASE....这里先提供spring-data-redis-1.7.2.RELEASE.jar
10. **源码分析**:`spring-data-redis-1.7.6.RELEASE-sources.jar`包含了源码,对于开发者来说,这是一个宝贵的资源,可以深入理解其内部实现,提高解决问题的能力。 总的来说,Spring Data Redis 1.7.6版本提供了...
spring-data-redis-2.0.0.M1.jar
spring-data-redis-1.4.1.RELEASE
spring-data-redis-1.6.2.RELEASE.jar,官网文档,免费提供,亲测可用
而Spring Data Redis是Spring Framework的一个模块,专门用于简化与Redis键值存储系统的交互。Redis是一个高性能的、开源的、支持网络的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 在...
例如,要使用Spring Data Redis,首先需要在项目中引入`spring-data-redis-1.6.0.RELEASE.jar`。然后,通过配置文件设置Redis服务器的连接信息,包括主机地址、端口、密码等。接着,在Spring的配置类中定义...
基于spring的子项目spring-data-redis写的一个基于hash类型的用户CRUD,经过简单的封装,可以实现通用CRUD,请事先安装好redis,配置文件redis.properties请做相应修改,希望对你有帮助。