文章目录
- 前言
- Java客户端Jedis
- 准备工作
- 引入Jedis
- Jedis的基本使用方法
- String类型的操作
- List类型操作
- Hash类型操作
- Set类型操作
- Zet类型的操作
- JavaSpring客户端
- 创建项目引入SpringDataRedis
- 配置 redis 服务地址
- 创建 Controller
- 具体实例
前言
Redis是用单线程来处理多个客户端的访问,因此作为Redis的开发和运维人员需要了解Redis服务端和客户端的通信协议,以及主流编程语言的Redis客户端使用方法,同时还需要了解客户端管理的相应API以及开发运维中可能遇到的问题。
Java客户端Jedis
准备工作
在进行java客户端的连接之前,我们要解决一个访问的问题,我们怎么才能访问到linux云服务器的redis,正常情况下,有人会说,我们直接开放redis的接口不就好了,答案是不能,因为如果我们开放了接口,那么就会让不怀好意的人根据我们的接口进行攻击,这样我们的内存就有可能爆炸。
所以我们只能外网访问。我下面提供了两种思路:
- 直接让java程序,也在linux上运行。
- 配置ssh端口转发,把云服务器的redis端口映射到本机上。
具体的配置请看以下操作。
配置好之后,我们可以代开cmd,看看映射端口有没有
这样我们准备工作就做完了。
引入Jedis
创建一个maven项目,引入Jedis依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.7</version>
</dependency>
Jedis的基本使用方法
public class RedisDemoGeneric {
public static void test1(Jedis jedis){
System.out.println("get 和 set的使用");
//先清空数据库,要避免上一组测试的残留数据影响到下一组测试的结果
jedis.flushAll();
jedis.set("key","111");
jedis.set("key2","222");
SetParams params =new SetParams();
params.ex(10);
params.xx();
jedis.set("key","333",params);
String value= jedis.get("key");
System.out.println("value= "+value);
}
public static void test2(Jedis jedis){
System.out.println("exist 和 del");
jedis.flushAll();
jedis.set("key","111");
jedis.set("key2","222");
Boolean result=jedis.exists("key");
System.out.println("result="+result);
long result2= jedis.del("key");
System.out.println(result2);
result=jedis.exists("key");
System.out.println("result="+result);
result2=jedis.del("key","key2","key3");
System.out.println(result2);
}
public static void test3(Jedis jedis){
System.out.println("keys");
jedis.flushAll();
jedis.set("key1","111");
jedis.set("key2","111");
jedis.set("key3","111");
jedis.set("key4","111");
Set<String>keys= jedis.keys("*");
System.out.println(keys);
}
public static void test4(Jedis jedis){
System.out.println("expire 和 ttl");
jedis.flushAll();
jedis.set("key","111");
jedis.expire("key",10);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
long time=jedis.ttl("key");
System.out.println("time =" +time);
}
public static void test5(Jedis jedis){
System.out.println("type");
jedis.set("key","111");
String type=jedis.type("key");
System.out.println(type);
jedis.lpush("key2","111","222","333");
type=jedis.type("key2");
System.out.println("type :"+type);
jedis.hset("key3","f1","1111");
type=jedis.type("key3");
System.out.println("type :"+type);
jedis.sadd("key4","111","222","333");
type=jedis.type("key4");
System.out.println("type :"+type);
jedis.zadd("key5",10,"zhangsan");
type=jedis.type("key5");
System.out.println("type :"+type);
}
public static void main(String[] args) {
//1.连接到redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
// //redis的各种命令,就都对应到jedis对象的各种方法
// String pong= jedis.ping();
// System.out.println(pong);
// test1(jedis);
// test2(jedis);
// test3(jedis);
// test4(jedis);
test5(jedis);
}
}
}
String类型的操作
public class RedisDemoString {
public static void test1(Jedis jedis){
jedis.flushAll();
jedis.mset("key1","111","key2","222","key3","333");
List<String>valve= jedis.mget("key1","key2","key3","key300");
System.out.println("valves ="+valve);
}
public static void test2(Jedis jedis){
System.out.println("getrange 和 setrange");
jedis.flushAll();
jedis.set("key","abcdefghijk");
String result= jedis.getrange("key",2,5);
System.out.println("result:"+result);
jedis.setrange("key",2,"xyz");
String value=jedis.get("key");
System.out.println("value="+value);
}
public static void test3(Jedis jedis){
System.out.println("append");
jedis.flushAll();
jedis.set("key","abcdefg");
jedis.append("key","ghij");
String value=jedis.get("key");
System.out.println("value"+value);
}
public static void test4(Jedis jedis){
System.out.println("incr 和 decr");
jedis.flushAll();
jedis.set("key","100");
long result=jedis.incr("key");
System.out.println(result);
String value=jedis.get("key");
System.out.println(value);
result=jedis.decr("key");
value=jedis.get("key");
System.out.println("value= "+value);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
// test1(jedis);
// test2(jedis);
// test3(jedis);
test4(jedis);
}
}
}
List类型操作
public class RedisDemoList {
public static void test1(Jedis jedis){
System.out.println("lpush 和 lrange");
jedis.flushAll();
jedis.lpush("key","111","222","333");
List<String>result=jedis.lrange("key",0,-1);
System.out.println(result);
}
public static void test2(Jedis jedis){
System.out.println("rpush 和 lrange");
jedis.flushAll();
jedis.rpush("key","111","222","333");
List<String>result=jedis.lrange("key",0,-1);
System.out.println(result);
}
public static void test3(Jedis jedis){
System.out.println("lpop");
jedis.flushAll();
jedis.rpush("key","111","222","333");
String result =jedis.lpop("key");
System.out.println(result);
result =jedis.lpop("key");
System.out.println(result);
result =jedis.lpop("key");
System.out.println(result);
result =jedis.lpop("key");
System.out.println(result);
}
public static void test4(Jedis jedis){
System.out.println("rpop");
jedis.flushAll();
jedis.rpush("key","111","222","333");
String result =jedis.rpop("key");
System.out.println(result);
result =jedis.rpop("key");
System.out.println(result);
result =jedis.rpop("key");
System.out.println(result);
result =jedis.rpop("key");
System.out.println(result);
result =jedis.rpop("key");
System.out.println(result);
}
public static void test5(Jedis jedis){
System.out.println("blpop");
jedis.flushAll();
//返回的结果是一个二元组,一个是从哪个key 对应的list中删除的,一个是删除的元素是什么。
List<String> result=jedis.blpop(100,"key");
System.out.println("result[0] "+result.get(0));
System.out.println("result[1] "+result.get(1));
}
public static void test6(Jedis jedis){
System.out.println("len");
jedis.flushAll();
jedis.rpush("key","111","222","333");
long len =jedis.llen("key");
System.out.println(len);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis=jedisPool.getResource()){
// test1(jedis);
// test2(jedis);
// test3(jedis);
// test5(jedis);
test6(jedis);
}
}
}
Hash类型操作
public class RedisDemoHash {
public static void test1(Jedis jedis){
System.out.println("hset 和 hget");
jedis.flushAll();
jedis.hset("key","f1","111");
Map<String,String> fields=new HashMap<>();
fields.put("f2","222");
fields.put("f3","333");
jedis.hset("key",fields);
String result=jedis.hget("key","f1");
System.out.println(result);
result=jedis.hget("key","f2");
System.out.println(result);
result=jedis.hget("key","f100");
System.out.println(result);
}
public static void test2(Jedis jedis){
System.out.println("hexist");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","111");
jedis.hset("key","f3","111");
Boolean result= jedis.hexists("key","f1");
System.out.println(result);
}
public static void test3(Jedis jedis){
System.out.println("hdel");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","111");
jedis.hset("key","f3","111");
long result =jedis.hdel("key","f1","f2");
System.out.println(result);
}
public static void test4(Jedis jedis){
System.out.println("hkeys 和 hvals");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","111");
jedis.hset("key","f3","111");
Set<String>fileds= jedis.hkeys("key");
List<String>vals=jedis.hvals("key");
System.out.println("field +"+fileds);
System.out.println("vals "+vals);
}
public static void test5(Jedis jedis){
System.out.println("hmget 和 hmset");
jedis.flushAll();
Map<String,String>map=new HashMap<>();
map.put("f1","111");
map.put("f2","111");
map.put("f3","111");
jedis.hmset("key",map);
List<String>values= jedis.hmget("key","f1","f2","f3");
System.out.println(values);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
// test1(jedis);
// test3(jedis);
// test4(jedis);
test5(jedis);
}
}
}
Set类型操作
public class RedisDemoSet {
public static void test1(Jedis jedis){
System.out.println("sadd 和 smembers");
jedis.flushAll();
jedis.sadd("key","111","222","333");
Set<String> result=jedis.smembers("key");
System.out.println(result);
}
public static void test2(Jedis jedis){
System.out.println("sismember");
jedis.sadd("key","111","222","333");
Boolean result=jedis.sismember("key","111");
System.out.println("result "+result);
}
public static void test3(Jedis jedis){
System.out.println("scard");
jedis.flushAll();
jedis.sadd("key","111","222","333");
long result=jedis.scard("key");
System.out.println(result);
}
public static void test4(Jedis jedis){
jedis.flushAll();
System.out.println("spop");
jedis.sadd("key","111","222","333","444","555");
String result =jedis.spop("key");
System.out.println(result);
}
public static void test5(Jedis jedis){
System.out.println("sinter");
jedis.flushAll();
jedis.sadd("key1","111","222","333");
jedis.sadd("key2","111","222","444");
Set<String> result=jedis.sinter("key1","key2");
System.out.println(result);
}
public static void test6(Jedis jedis){
System.out.println("sinterstore");
jedis.flushAll();
jedis.sadd("key1","111","222","333");
jedis.sadd("key2","111","222","444");
long len =jedis.sinterstore("key3","key1","key2");
System.out.println(len);
Set<String> reseult=jedis.smembers("key3");
System.out.println("result"+reseult);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
// test1(jedis);
// test2(jedis);
// test3(jedis);
// test4(jedis);
// test5(jedis);
test6(jedis);
}
}
}
Zet类型的操作
public class RedisDemoZet {
public static void test1(Jedis jedis){
System.out.println("zadd 和 zrange");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
Map<String,Double> map= new HashMap<>();
map.put("lisi",20.0);
map.put("wangwu",30.0);
jedis.zadd("key",map);
List<String> members= jedis.zrange("key",0,-1);
System.out.println(members);
List<Tuple> memberWithScore= jedis.zrangeWithScores("key",0,-1);
System.out.println(memberWithScore);
String member=memberWithScore.get(0).getElement();
Double score=memberWithScore.get(1).getScore();
System.out.println("member :" + member+","+ score +"+score");
}
public static void test2(Jedis jedis){
System.out.println("zcard");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
long count=jedis.zcard("key");
System.out.println(count);
}
public static void test3(Jedis jedis){
System.out.println("zrem");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
long n= jedis.zrem("key","zhangsan");
System.out.println(n);
List<Tuple> memberWithScore= jedis.zrangeWithScores("key",0,-1);
System.out.println(memberWithScore);
}
public static void test4(Jedis jedis){
System.out.println("zscore");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
Double score=jedis.zscore("key","zhangsan");
System.out.println(score);
}
public static void test5(Jedis jedis){
System.out.println("zrank");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
Long rank= jedis.zrank("key","zhangsan");
System.out.println(rank);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
// test1(jedis);
// test2(jedis);
// test3(jedis);
// test4(jedis);
test5(jedis);
}
}
}
JavaSpring客户端
创建项目引入SpringDataRedis
配置 redis 服务地址
spring:
redis:
host: 127.0.0.1
port: 8888
创建 Controller
public class MyController {
@Autowired
private StringRedisTemplate redisTemplate;
}
这里面的StringRedisTemplate,实际上存放着操作redis的接口。
具体实例
@RestController
public class MyController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/testString")
public String testString(){
redisTemplate.opsForValue().set("key","111");
redisTemplate.opsForValue().set("key2","222");
redisTemplate.opsForValue().set("key3","333");
String value=redisTemplate.opsForValue().get("key");
System.out.println("value "+value);
return "OK";
}
@GetMapping("/testlist")
@ResponseBody
public String testList(){
//先清除缓存
redisTemplate.execute((RedisConnection connection)->{
//execute要求回调方法必须写return 语句。
//这个回调返回的对象化,就会作为execute本身的返回值。
connection.flushAll();
return null;
});
redisTemplate.opsForList().leftPush("key","111");
redisTemplate.opsForList().leftPush("key","222");
redisTemplate.opsForList().leftPush("key","333");
String value=redisTemplate.opsForList().rightPop("key");
System.out.println("value " +value);
value=redisTemplate.opsForList().rightPop("key");
System.out.println("value " +value);
value=redisTemplate.opsForList().rightPop("key");
System.out.println("value " +value);
return "Ok";
}
@GetMapping("/testSet")
@ResponseBody
public String testSet(){
//先清除缓存
redisTemplate.execute((RedisConnection connection)->{
//execute要求回调方法必须写return 语句。
//这个回调返回的对象化,就会作为execute本身的返回值。
connection.flushAll();
return null;
});
redisTemplate.opsForSet().add("key", "aaa", "bbb", "ccc");
Set<String> result=redisTemplate.opsForSet().members("key");
System.out.println("result: "+ result);
boolean ok = redisTemplate.opsForSet().isMember("key", "aaa");
System.out.println(ok);
redisTemplate.opsForSet().remove("key", "aaa");
long n = redisTemplate.opsForSet().size("key");
return "ok";
}
@GetMapping("testHash")
@ResponseBody
public String testHash(){
redisTemplate.execute((RedisConnection connection) ->{
connection.flushAll();
return null;
});
redisTemplate.opsForHash().put("key","f1","111");
redisTemplate.opsForHash().put("key","f2","111");
redisTemplate.opsForHash().put("key","f3","111");
String value=(String) redisTemplate.opsForHash().get("key","f1");
System.out.println("value: "+value);
Boolean exists=redisTemplate.opsForHash().hasKey("key","f1");
System.out.println("exists: "+exists);
redisTemplate.opsForHash().delete("key","f1","f2");
Long size=redisTemplate.opsForHash().size("key");
System.out.println("size:" +size);
return "ok";
}
@GetMapping("testZet")
@ResponseBody
public String testZSet(){
redisTemplate.execute((RedisConnection connection) ->{
connection.flushAll();
return null;
});
redisTemplate.opsForZSet().add("key","zhangsan",10);
redisTemplate.opsForZSet().add("key","lisi",10);
redisTemplate.opsForZSet().add("key","wangwu",10);
Set<String> member= redisTemplate.opsForZSet().range("key",0,-1);
System.out.println(member);
Set<ZSetOperations.TypedTuple<String>> memberWithScores= redisTemplate.opsForZSet().rangeWithScores("key",0,-1);
System.out.println(memberWithScores);
Double score=redisTemplate.opsForZSet().score("key","zhangsan");
System.out.println("score: "+score);
redisTemplate.opsForZSet().remove("key","zhangsan");
Long size=redisTemplate.opsForZSet().size("key");
System.out.println("size: "+size);
Long rank =redisTemplate.opsForZSet().rank("key","lisi");
System.out.println("rank :"+rank);
return "ok";
}
}