- 配置连接
- StringRedisTemplate和RedisTemplate
- RedisView
- QuickRedis
- 下载地址
- Another Redis DeskTop Manager
使用
配置连接
引入依赖就略过了,idea创建项目时候可以选择相应组件。application.properties配置:
#指定redis信息 (如 host, ip, password)
spring.redis.host=localhost
spring.redis.port=6379
#没有密码可以不用配置这个
#spring.redis.password=123456
代码使用
Controller文件
package com.chen.classify;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class RedisController {
/**
* 需要注入redis模板
*
* 对于RedisTemplate的泛型情况,
* 可以使用<String, String>
* <Object, Object>
* 或者不写泛型
*
* 注意,属性的名称必须为redisTemplate,因为按名称注入,框架创建的对象就是这个名字的
*/
@Resource
private RedisTemplate redisTemplate;
@Resource
private StringRedisTemplate stringRedisTemplate;
// 添加数据到redis
@PostMapping("/redis/addstring")
public String addToRedis(String name, String value) {
// 操作Redis中的string类型的数据,先获取ValueOperation
ValueOperations valueOperations = redisTemplate.opsForValue();
// 添加数据到redis
valueOperations.set(name, value);
return "向redis添加string类型的数据";
}
// 从redis获取数据
@GetMapping("/redis/getk")
public String getData(String key) {
ValueOperations valueOperations = redisTemplate.opsForValue();
Object v = valueOperations.get(key);
return "key是" + key + ",它的值是:" + v;
}
@PostMapping("/redis/{k}/{v}")
public String addStringKV(@PathVariable String k,
@PathVariable String v) {
// 使用StringRedisTemplate对象
stringRedisTemplate.opsForValue().set(k,v);
return "使用StringRedisTemplate对象添加";
}
@GetMapping("/redis/{k}")
public String getStringValue(@PathVariable String k) {
// 获取String类型的value
String v = stringRedisTemplate.opsForValue().get(k);
return "从redis中通过" + k + "获取到string类型的v=" + v;
}
/** 设置RedisTemplate序列化机制
* 可以设置 key 的序列化,也可以设置 value 的序列化
* 也可以同时设置
*/
@PostMapping("/redis/addstr")
public String addString(String k, String v) {
// 设置RedisTemplate的序列化机制
// 设置key为string类型的序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 设置value的序列化
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.opsForValue().set(k, v);
return "添加了k和v";
}
/**
* 使用json序列化
*/
@PostMapping("/redis/addjson")
public String addJson() {
Student student = new Student();
student.setName("zhangsan");
student.setAge(20);
student.setId(1);
// 设置key为string的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 设置value为json的序列化方式,json为Student类型的方式组织,所以需要传入Student.class
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));
redisTemplate.opsForValue().set("myStudent", student);
return "存入json类型的数据student";
}
}
实体类
package com.chen.classify;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = -7839813688155519106L;
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
StringRedisTemplate和RedisTemplate
上面说到了这两者在存取中的差异
StringRedisTemplate : 这个类将key和value都做String处理,使用的是String的序列化,可读性好
RedisTemplate : 把key和value经过了序列化,key和value都是序列化的内容,不能直接识别,默认使用的是JDK的序列化,可以修改为其他的序列化
序列化作用 :
序列化是将对象转换为可传输字节序列的过程,反序列化是将字节序列还原为原对象的过程.序列化最终的目的是为了对象可以跨平台存储和进行网络的传输
序列化的方式 :
序列化只是一种拆装对象的规则,那么这种规则也就多种多样,常见的有JDK(不支持跨语言),json,xml,Hessian等
我们上面的RedisTemplate类的存储就是JDK方式的
java的序列化 : 把java对象转换为byte[],二进制数据
json序列化 : json序列化功能将对象转换为json格式或者将其转换回对象,如Student对象转换为{“name”:“张三”,“age”:“20”}
参考:springboot使用redis_迷亭君的博客-CSDN博客_redis在springboot中的使用
Redis连接工具/客户端
MySQL可以使用Navicat等连接工具,但是能连接redis的并不多,接下来介绍两款,并附上下载地址。
RedisView
RedisView是一个redis数据库GUI工具,提供简单的添加,删除,更改和基本命令,支持集群模式。复制集或群集模式只需填写 IP 和端口。
1、Redis数据库视图工具,提供CURD功能
2、提供基本命令运行
3、支持单例、复制集、哨兵、集群模式
4、支持订阅发布模式
5、支持批量删除、oracle与mysql导入导出、Oracle与mysql表键删除
6、支持中英文、设置编码、设置皮肤
7、支持千万级数据操作
源代码:https://github.com/cc20110101/RedisView
更多使用信息:https://blog.csdn.net/cc20110101/article/details/87301562
下载:RedisView 下载|SourceForge.net
QuickRedis
QuickRedis 是一款 永久免费 的 Redis 可视化管理工具。它支持直连、哨兵、集群模式,支持亿万数量级的 key,还有令人兴奋的 UI。QuickRedis 支持 Windows 、 Mac OS X 和 Linux 下运行。
QuickRedis 是一个效率工具,当别人在努力敲命令的时候,而你已经在喝茶。
Github:https://github.com/quick123official/quick_redis_blog
下载地址
https://gitee.com/quick123official/quick_redis_blog/releases/
https://github.com/quick123official/quick_redis_blog/releases/
Another Redis DeskTop Manager
Another Redis DeskTop Manager 是 GitHub 上的一个开源项目,是 Redis 可视化管理的利器,提供在 Windows、MacOS 平台的安装包,体积小,完全免费。
github:https://github.com/qishibo/AnotherRedisDesktopManager