Jedis

  Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis。

Spring Data Redis

  spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

​ spring-data-redis针对jedis提供了如下功能:

  1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类

  2. 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口

    • ValueOperations:简单K-V操作

    • SetOperations:set类型数据操作

    • ZSetOperations:zset类型数据操作

    • HashOperations:针对map类型的数据操作

    • ListOperations:针对list类型的数据操作

简单案例:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent_demo</artifactId>
<groupId>cn.zy.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>spring_data_redis_demo</artifactId> <!-- 集中定义依赖版本号 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties> <dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency> <!-- redis做缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency> </dependencies> <build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> </plugins> </build>
</project>

resources文件:

redis-config.properties

# Redis settings
# 服务器 IP
redis.host=192.168.44.32
# 端口
redis.port=6379
# 密码
redis.pass=
# 使用的dbIndex
redis.database=0
# 最大空闲数
redis.maxIdle=300
# 连接时的最大等待毫秒数
redis.maxWait=3000
# 在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true

applicationContext-redis.xml

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath*:properties/*.properties"/> <!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:database="${redis.database}"
p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory"/>
</bean> </beans>

连接Redis-Cluster的配置文件

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:properties/redis-cluster-config.properties" />
<bean id="redis-clusterConfiguration" class="org.springframework.data.redis.connection.redis-clusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>
<bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<constructor-arg ref="redis-clusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory" />
</bean>
</beans>

redis-cluster-config.properties

#cluster configuration
redis.host1=192.168.25.140
redis.port1=7001 redis.host2=192.168.25.140
redis.port2=7002 redis.host3=192.168.25.140
redis.port3=7003 redis.host4=192.168.25.140
redis.port4=7004 redis.host5=192.168.25.140
redis.port5=7005 redis.host6=192.168.25.140
redis.port6=7006 redis.maxRedirects=3
redis.maxIdle=100
redis.maxTotal=600

测试:

/**
* 值类型操作
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestValue { @Autowired
private RedisTemplate redisTemplate; @Test
public void setValue() throws Exception {
redisTemplate.opsForValue().set("name", "tom");
redisTemplate.boundValueOps("age").set("22", 10, TimeUnit.SECONDS);//带过期时间的
} @Test
public void getValue() throws Exception {
String age = (String) redisTemplate.opsForValue().get("age");
String name = (String) redisTemplate.boundValueOps("name").get();
System.out.println(name);
} @Test
public void delete() throws Exception {
redisTemplate.delete("name");
}
}
/**
* set类型操作
*@Param
*@return
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
//set1:['','']
@Test
public void setValue() throws Exception {
redisTemplate.boundSetOps("set1").add("刘备");
redisTemplate.boundSetOps("set1").add("曹操");
redisTemplate.boundSetOps("set1").add("孙权");
} @Test
public void getValue() throws Exception {
Set set = redisTemplate.boundSetOps("set1").members();
System.out.println(set);
} @Test
public void remove() throws Exception {
redisTemplate.boundSetOps("set1").remove("曹操");
} @Test
public void delete() throws Exception {
redisTemplate.delete("set1");
}
}
/**
* list类型操作
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
private RedisTemplate redisTemplate; // list1 : [] @Test
public void setValue1() throws Exception {
redisTemplate.boundListOps("list1").leftPush("唐僧");
redisTemplate.boundListOps("list1").leftPush("悟空");
redisTemplate.boundListOps("list1").leftPush("八戒");
redisTemplate.boundListOps("list1").leftPush("沙僧");
} @Test
public void getValue1() throws Exception {
List list = redisTemplate.boundListOps("list1").range(0, 10);
System.out.println(list);
} @Test
public void setValue2() throws Exception {
redisTemplate.boundListOps("list2").rightPush("大乔");
redisTemplate.boundListOps("list2").rightPush("小乔");
redisTemplate.boundListOps("list2").rightPush("孙尚香");
} @Test
public void getValue2() throws Exception {
List list = redisTemplate.boundListOps("list2").range(0, 10);
System.out.println(list);
} @Test
public void getByIndex() throws Exception {
String name = (String) redisTemplate.boundListOps("list1").index(1);
System.out.println(name);
} @Test
public void remove() throws Exception {
redisTemplate.boundListOps("list1").remove(2,"八戒");
} }
/**
* hash类型操作
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
//specList:{key:value} @Test
public void setValue() throws Exception {
redisTemplate.boundHashOps("map1").put("a","刘备");
redisTemplate.boundHashOps("map1").put("b","关羽");
redisTemplate.boundHashOps("map1").put("c","张飞");
} @Test
public void getKeys() throws Exception {
Set set = redisTemplate.boundHashOps("map1").keys();
System.out.println(set);
} @Test
public void getValues() throws Exception {
List list = redisTemplate.boundHashOps("map1").values();
System.out.println(list);
} @Test
public void getValue() throws Exception {
String name = (String) redisTemplate.boundHashOps("map1").get("b");
System.out.println(name);
} @Test
public void remove() throws Exception {
redisTemplate.boundHashOps("map1").delete("c");
} @Test
public void delete() throws Exception {
redisTemplate.delete("map1");
}
}

SpringDataRedis操作Redis简单案例的更多相关文章

  1. Spring-data-redis操作redis cluster

    Redis 3.X版本引入了集群的新特性,为了保证所开发系统的高可用性项目组决定引用Redis的集群特性.对于Redis数据访问的支持,目前主要有二种方式:一.以直接调用jedis来实现:二.使用sp ...

  2. Spring-data-redis操作redis知识汇总

    什么是spring-data-redis spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作,使用java操作redis最常用 ...

  3. Spring-data-redis操作redis知识总结

    什么是spring-data-redis spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作,使用java操作redis最常用 ...

  4. Java操作redis简单示例

    第一:安装Redis    首先我们要安装Redis,就像我们操作数据库一样,在操作之前肯定要先创建好数据库的环境.    Redis的下载可以百度一下,或者打开下面的下载链接:    https:/ ...

  5. php操作redis简单例子

    <?php //在PHP里操作Redis //Redis就是php的一个功能类 //创建Redis对象 $redis = new Redis(); //链接redis服务器 $redis -&g ...

  6. Redis简单案例(二) 网站最近的访问用户

    我们有时会在网站中看到最后的访问用户.最近的活跃用户等等诸如此类的一些信息.本文就以最后的访问用户为例, 用Redis来实现这个小功能.在这之前,我们可以先简单了解一下在oracle.sqlserve ...

  7. Redis简单案例(三) 连续登陆活动的简单实现

    连续登陆活动,或许大家都不会陌生,简单理解就是用户连续登陆了多少天之后,系统就会送一些礼品给相应的用户.最常见的 莫过于游戏和商城这些.游戏就送游戏币之类的东西,商城就送一些礼券.正值国庆,应该也有不 ...

  8. Redis简单案例(一) 网站搜索的热搜词

    对于一个网站来说,无论是商城网站还是门户网站,搜索框都是有一个比较重要的地位,它的存在可以说是 为了让用户更快.更方便的去找到自己想要的东西.对于经常逛这个网站的用户,当然也会想知道在这里比较“火” ...

  9. Redis简单案例(四) Session的管理

    负载均衡,这应该是一个永恒的话题,也是一个十分重要的话题.毕竟当网站成长到一定程度,访问量自然也是会跟着增长,这个时候, 一般都会对其进行负载均衡等相应的调整.现如今最常见的应该就是使用Nginx来进 ...

随机推荐

  1. access_ok | 检查用户空间内存块是否可用

    access_ok() 函数是用来代替老版本的 verify_area() 函数的.它的作用也是检查用户空间指针是否可用. 函数原型:access_ok (type, addr, size); 变量说 ...

  2. [置顶] 【机器学习PAI实践五】机器学习眼中的《人民的名义》

    一.背景 最近热播的反腐神剧"人民的名义"掀起来一波社会舆论的高潮,这部电视剧之所能得到广泛的关注,除了老戏骨们精湛的演技,整部剧出色的剧本也起到了关键的作用.笔者在平日追剧之余, ...

  3. [转]页游开发中的 Python 组件与模式Presentation Transcript

    转: 页游开发中的 Python 组件与模式Presentation Transcript 1. 页游开发中的 Python 组件与模式 赖勇浩( http://laiyonghao.com ) 20 ...

  4. I.MX6 AR8031 寄存器操作

    /*************************************************************************** * I.MX6 AR8031 寄存器操作 * ...

  5. 安装PL/SQL客户端来访问操作步骤

    1 2 3.改变路径 4 5 6 7 8 9.选择注册机 10

  6. iOS 10 隐私权限设置

    iOS 10 开始对隐私权限更加严格,如果你不设置就会直接崩溃,现在很多遇到崩溃问题了,一般解决办法都是在info.plist文件添加对应的Key-Value就可以了. 以上Value值,圈出的红线部 ...

  7. 学习动态性能表(2)--v$sesstat

    学习动态性能表 第二篇--v$sesstat  2007.5.25 按照OracleOnlineBook中的描述,v$sesstat存储session从login到logout的详细资源使用统计. 类 ...

  8. 题目1539:师弟 ——最短路+DFS

    题意::从起点到终点的所有的最短路中,找出离终点有X个路口的城市一共有几个 开始我用最短路+DFS从起点开始搜,超时了 换了一种方法,从终点开始搜,AC #include<stdio.h> ...

  9. 1、python基础速成

    基础模块 def prt(age,name):#函数定义 print("%s is %d 年龄 old"%(name,age)) if __name__=="__main ...

  10. linux下创建与删除用户详细步骤 ***

    linux下用户的操作还是相对容易理解的,基本操作如下: 1.新增用户 只有root用户能创建新用户 #useradd user1 新建后将会在/home目录下生成一个与用户名相同的用户主目录.同时会 ...