简介
Java客户端jedis的例子和SpringDataRedis的简介## Java客户端
常用的Java客户端有jedis,lettuce,redission。
优缺点:
jedis简单实用,api名是redis的命令,学习成本低。不过jedis实例的线程是不安全的,在多线程中要结合连接池使用。
lettuce是基于netty实现的,支持同步,异步,线程安全,并且支持redis的哨兵模式,集群模式和管道模式
redission:基于redis实现的分布式,可伸缩的Java数据结构集合。
jedis的简单例子
创建maven项目
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"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>redistest</artifactId><version>1.0-SNAPSHOT</version><name>redistest</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 添加jedis依赖 --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.0</version></dependency></dependencies><build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin><!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --><plugin><artifactId>maven-site-plugin</artifactId><version>3.7.1</version></plugin><plugin><artifactId>maven-project-info-reports-plugin</artifactId><version>3.0.0</version></plugin></plugins></pluginManagement></build>
</project>
test测试jedis
package org.redis;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;public class redistest {private Jedis jedis;@Beforepublic void linkRedis(){jedis = new Jedis("linux的ip地址",6379);jedis.auth("123456");jedis.select(0);}@Testpublic void stringTest(){String result = jedis.set("s4", "4");System.out.println(result);String s4 = jedis.get("s4");System.out.println(s4);}@Testpublic void hashTest(){jedis.hset("s5","name","christ");jedis.hset("s5","age","23");String name = jedis.hget("s5", "name");String age = jedis.hget("s5","age");System.out.println(name+age);}@Afterpublic void downRedis(){if(jedis != null){jedis.close();}}
}
结合连接池
在上面例子的基础上,新增一个JedisConnectionFactory类来构建连接池
package org.redis;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public class JedisConnectionFactory {private static final JedisPool jedisPool ;static {JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(8);poolConfig.setMaxIdle(8);poolConfig.setMinIdle(0);poolConfig.setMaxWaitMillis(100);jedisPool = new JedisPool(poolConfig,"linux的ip地址",6379,1000,"123456");}public static Jedis getJedisPool(){return jedisPool.getResource();}}
在上面例子的test类中linkRedis方法中,可以通过连接池获得jedis
@Beforepublic void linkRedis(){jedis = JedisConnectionFactory.getJedisPool();jedis.select(0);}
SpringDataRedis
springdata是spring数据操作的模块,集成了各种数据库,SpringDataRedis就是对redis的集成模块。
特点:
- 提供了对不同Redis客户端的整合(Lettuce和Jedis)
- 提供了RedisTemplate统一API来操作Redis
- 支持Redis的发布订阅模型
- 支持Redis哨兵和Redis集群
- 支持基于Lettuce的响应式编程
- 支持基于JDK、JSON、字符串、Spring对象的数据序列化及反序列化
- 支持基于Redis的JDKCollection实现
redistemplete
SpringDataRedis提供的工具,封装了各种对redis的操作,并且将不同的数据类型的操作api封装到了不同的类型中。
api | 操作类型 |
---|---|
redisTemplete.opsForValue() | string |
redisTemplete.opsForHash() | hash |
redisTemplete.opsForList() | list |
redisTemplete.opsForSet() | set |
redisTemplete.opsForZSet() | soctedset |
redisTemplete | 通用的命令 |
序列化实践方案
方案一
自定义redisTemplate
修改RedisTemplate的序列化器为GenericJackson2JsonRedisSerializer
方案二
使用StringRedisTemplate
写入redis时,手动把对象序列化为json
读取redis时,手动把读取到的json反序列为对象。