Redis 在本地安装与用java测试

news/2025/2/9 11:13:46/

1. 在window上安装Redis

1. 下载Redis

  • Github下载地址:Releases · microsoftarchive/redis · GitHub

Redis-x64-3.0.504.zip

2. 解压到某个目录下,我的在c:/apps/redis

3. 启动redis server。

C:\apps\redis>redis-server redis.windows.conf_.__.-``__ ''-.__.-``    `.  `_.  ''-._           Redis 3.0.504 (00000000/0) 64 bit.-`` .-```.  ```\/    _.,_ ''-._(    '      ,       .-`  | `,    )     Running in standalone mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379|    `-._   `._    /     _.-'    |     PID: 39100`-._    `-._  `-./  _.-'    _.-'|`-._`-._    `-.__.-'    _.-'_.-'||    `-._`-._        _.-'_.-'    |           http://redis.io`-._    `-._`-.__.-'_.-'    _.-'|`-._`-._    `-.__.-'    _.-'_.-'||    `-._`-._        _.-'_.-'    |`-._    `-._`-.__.-'_.-'    _.-'`-._    `-.__.-'    _.-'`-._        _.-'`-.__.-'[39100] 22 May 14:48:13.225 # Server started, Redis version 3.0.504
[39100] 22 May 14:48:13.229 * The server is now ready to accept connections on port 6379

4. 打开另外命令窗口启动Redis command

命令是redis-cli.exe.  

set name hello 是保持一个名字是name,值是hello的数据

get name:  是取出name的值

C:\apps\redis>redis-cli.exe
127.0.0.1:6379> set name hello
OK
127.0.0.1:6379> get name
"hello"
127.0.0.1:6379> get name

keys *: 取出所有的取出说有的key.

127.0.0.1:6379> keys *
1) "Student:liu2"
2) "Student:liu5"
3) "Student"
4) "Student:liu3"
5) "name"

hmset 保存数据,用hget取数据

redis> HMSET myhash field1 "Hello" field2 "World"
"OK"
redis> HGET myhash field1
"Hello"
redis> HGET myhash field2
"World"

hmset保持数据。key是application

HMSET application sample.property.name1 "somevalue" sample.property.name2 "anothervalue"

hgetall 去application的数据

127.0.0.1:6379> hgetall application
1) "sample.property.name1"
2) "somevalue"
3) "sample.property.name2"
4) "anothervalue"127.0.0.1:6379> hget application sample.property.name1
"somevalue"
127.0.0.1:6379> hget application sample.property.name2
"anothervalue"

 刪除某一個key

127.0.0.1:6379> del certainkey
(integer) 1

按某种规则删除一批数据

KEYS "prefix:*" | xargs DEL

2. Java spring代码测试Redis

在pom.xml加maven依赖

	<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.10</version><relativePath /> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><type>jar</type></dependency></dependencies>

创建Configuration

@Configuration
public class RedisConfiguration {@Beanpublic RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(jedisConnectionFactory());return template;}@BeanJedisConnectionFactory jedisConnectionFactory() {JedisConnectionFactory jedisConFactory= new JedisConnectionFactory();jedisConFactory.setHostName("localhost");jedisConFactory.setPort(6379);return jedisConFactory;}
}

创建Student 类

@RedisHash("Student")
public class Student implements Serializable {public enum Gender { MALE, FEMALE}private String id;private String name;private Gender gender;private int grade;// ...
}

创建Redis respository

@Repository
public interface StudentRepository extends CrudRepository<Student, String> {}

创建StudentDataService

@Service
public class StudentDataService {@Autowiredprivate StudentRepository studentRepository;public Student saveStudent(String id) {Student student = new Student(id, "John Doe", Student.Gender.MALE, 1);student = studentRepository.save(student);return student;}public Student getStudent(String id) {Student retrievedStudent = studentRepository.findById(id).get();return retrievedStudent;}public Student updateStudent(Student student) {Student dbStduent = this.getStudent(student.getId());dbStduent.setName(student.getName());dbStduent = studentRepository.save(dbStduent);return dbStduent;}public void deleteStudent(Student student) {studentRepository.deleteById(student.getId());}public List<Student> getAllStudents() {List<Student> students = new ArrayList<>();studentRepository.findAll().forEach(students::add);return students;}}

最后创建StudentController用于测试

@RestController
@RequestMapping("/student")
public class StudentContoller {@Autowiredprivate StudentDataService studentDataService;@RequestMapping("/save/{id}")public String saveStudent(@PathVariable("id") String id) {//"Eng2015001"Student student = studentDataService.saveStudent(id);return student.toString();}@RequestMapping("/get/{id}")public String getStudent(@PathVariable("id") String id) {Student student = studentDataService.getStudent(id);return student.toString();}@RequestMapping("/delete/{id}")public String deleteStudent(@PathVariable("id")String id) {Student student = new Student();student.setId(id);studentDataService.deleteStudent(student);return "success";}@RequestMapping("/update/{id}")public String updateStudent(@PathVariable("id")  String id) {Student student = studentDataService.getStudent(id);student.setName("updated1");student = studentDataService.updateStudent(student);return student.toString();}@RequestMapping("/getAll")public String getAllStudents() {List<Student> students= studentDataService.getAllStudents();return students.toString();}}

参考资料

Introduction to Spring Data Redis | Baeldung


http://www.ppmy.cn/news/86222.html

相关文章

PHP 模块

php 扩展主要分为两类&#xff1a; php模块扩展和zend模块扩展。 php模块扩展 bz2 用于透明地读写 bzip2&#xff08;.bz2&#xff09;压缩文件。 安装--with-bz2[DIR] calendar 日历 安装--enable-calendar ctype 字符检测 此扩展默认为启用&#xff0c;编译时可通过下列选项…

利用qsort排序

一、简单排序10个元素的一维数组 #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable:6031) #include<stdio.h> #include<stdlib.h> void print_arr(int arr[], int sz) {int i 0;for (i 0; i < sz; i){printf("%d ", arr[i]);}printf("…

这样做WhatsApp群组营销,转化率猛UP

WhatsApp群组营销是一种利用WhatsApp群组进行推广和营销活动的策略。通过创建或参与相关主题的群组&#xff0c;您可以与潜在客户建立联系&#xff0c;传递信息并促进销售。 以下是一些WhatsApp群组营销的建议&#xff1a; 1.确定目标受众&#xff0c;建立目标群组&#xff1a…

STL--mapset(手撕AVL树,红黑树)

1. 关联式容器 在初阶阶段&#xff0c;我们已经接触过STL中的部分容器&#xff0c;比如&#xff1a;vector、list、deque、 forward_list(C11)等&#xff0c;这些容器统称为序列式容器&#xff0c;因为其底层为线性序列的数据结构&#xff0c;里面 存储的是元素本身。那什么是关…

探究弹性伸缩技术在云计算中的应用及其挑战

随着云计算技术的不断发展&#xff0c;人们对于云计算的认识和理解也在不断深入。作为云计算核心技术之一&#xff0c;弹性伸缩是云计算中一个重要的概念。它是指根据需求对云计算资源进行自动化的增加或减少&#xff0c;以实现资源的最佳利用和效率。弹性伸缩是云计算中实现高…

有没有想过一种可能,30岁之后,转行去做IT售前?

灵魂拷问 IT行业的变化是非常迅速的&#xff0c;各种新技术、新产品、新观念、新的业务模式层出不穷&#xff0c;不仅是我们&#xff0c;客户也在不断地学习进步&#xff0c;因此我们注定要终身学习。 IT售前这个岗位为许多IT职场人提供了一种新的选择: 你不需要成为某一方面…

初学QT:使用QtDesigner绘制一个简单的界面(Day01)

关于Qt 打算在这里记录我学习qt过程中遇见的问题的收获 今天是学习qt的第一天&#xff0c;首先找了一个界面打算照着这个界面写一个一样的 因为是第一天&#xff0c;所以我用的是qt designer写的 其中遇到的问题&#xff1a; 设置背景图片 首先不能直接添加图片到背景图片中…

PAT A1098 Insertion or Heap Sort

1098 Insertion or Heap Sort 分数 25 作者 CHEN, Yue 单位 浙江大学 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from th…