windows搭建redis服务

windows搭建redis服务

Windows 下 Redis 安装与配置 教程

下载软件

1、下载链接 https://github.com/microsoftarchive/redis/releases

安装教程,查看是否生效

1、安装包如图所示 Redis-x64-3.0.504.msi

2、双击 msi 安装包

双击 msi 安装程序,打开安装向导,点击 next

3、接受终端用户协议

接受终端用户协议,点击 next

4、选择安装路径

选择安装路径,并勾选将安装路径添加的系统 PATH 环境变量

5、设置服务端口

设置 Redis 服务端口,默认 6379,点击 next

6、设置最大内存限制

设置最大内存限制,点击 next

7、完成安装

点击 install 完成安装

8、验证 Redis

打开"任务管理器",可以看到服务列表下启动了 Redis 服务

9、命令行操作

打开 cmd 窗口,输入 redis-cli 连接 redis服务,并做简单验证

SpringBoot中集成Redis服务,实现入门基本操作

yml配置基本信息

yml 复制代码
  redis:
    host: localhost
    port: 6379
#    password: 0
    database: 0 # 几号库
    lettuce:
      pool:
        max-active: 8 # 最大连接
        max-idle: 8 # 最大空闲连接
        min-idle: 0 # 最小空闲连接
        max-wait: 100ms # 连接等待时间

spring对应redis缓存情况依赖

xml 复制代码
 <!--redis与pool2搭配使用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

单元测试

字符串类型

java 复制代码
// 添加操作
client.opsForValue().set(key, "v1", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS );
// 获取操作
String getValue = client.opsForValue().get(key);
// 累加
client.opsForValue().increment(counter);
// 减一
client.opsForValue().decrement(counter);
// 存储list<map>
String multiMapStr = JSON.toJSONString(multiMapList);
client.opsForValue().set("str:multiusers", multiMapStr, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);

hash类型

java 复制代码
// 添加单个字段元素
client.opsForHash().put("hash:user:single","name","pmb");
// 添加一个map
client.opsForHash().putAll("hash:user:all", handleMap);
// 返回key集合
Set<Object> keyList = client.opsForHash().keys("hash:user:all");
// 返回value集合
List<Object> valueList = client.opsForHash().values("hash:user:all");

list类型

java 复制代码
// 右侧添加元素
client.opsForList().rightPush(key, "16607024161");
// 右侧删除元素
client.opsForList().rightPop(key);
// 元素个数
client.opsForList().size(key);
// 根据指定下标查找元素
client.opsForList().index(key, 0);
// 获取某个范围元素
client.opsForList().range(key, 0, size - 1);
// 实现栈 先进后出
client.opsForList().leftPush(key,"1");
client.opsForList().leftPush(key,"2");
client.opsForList().leftPush(key,"3");
client.opsForList().leftPop(key);
client.opsForList().leftPop(key);
client.opsForList().leftPop(key);
// 实现队列 先进先出
client.opsForList().leftPush(key,"one");
client.opsForList().leftPush(key,"two");
client.opsForList().rightPop(key);
client.opsForList().rightPop(key);

set类型

java 复制代码
// 添加元素
client.opsForSet().add(key, "1","2","3");
// 返回所有元素
Set<String> members = client.opsForSet().members(key);
// 某个元素是否存在
Boolean member = client.opsForSet().isMember(key, "2");

client.opsForSet().add(intersection, "1","2");
// 交集
Set<String> intersectList = client.opsForSet().intersect(key, intersection);
// 并集
Set<String> unionList = client.opsForSet().union(key, intersection);
// 差集
Set<String> differenceList = client.opsForSet().difference(key, intersection);

zset类型

java 复制代码
// 顺序添加
client.opsForZSet().add(key, "one", 1);
// 返回所有元素
Set<String> range = client.opsForZSet().range(key, 0, size - 1);
// 根据score返回用户
Set<String> userList = client.opsForZSet().rangeByScore(key, 1, 60);
// 删除指定用户
client.opsForZSet().remove(key, "two");
// 获取指定score区间的用户个数
Long count = client.opsForZSet().count(key, 1, 20);
// 返回倒序的用户信息
Set<String> InvertedOrder = client.opsForZSet().reverseRange(key, 0, -1);
//
Set<ZSetOperations.TypedTuple<String>> allLis = client.opsForZSet().rangeWithScores(key, 0, -1);

案例说明

java 复制代码
package com.geekmice.springbootselfexercise.cache;

import com.geekmice.springbootselfexercise.SpringBootSelfExerciseApplication;
import com.geekmice.springbootselfexercise.constant.DateConstant;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: spring-boot-self-exercise
 * @BelongsPackage: com.geekmice.springbootselfexercise.cache
 * @Author: xxx
 * @CreateTime: 2023-12-09  03:04
 * @Description: 字符串操作缓存
 * @Version: 1.0
 */
@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
public class CacheTest {

    @Autowired
    private RedisTemplate<String,String> client;

    /**
     * 字符串类型缓存操作
     */
    @Test
    public void handleStrTest(){
        String key = "k1";
        String currentNum;
        // 用法1:key是否存在
        Boolean value = client.hasKey(key);
        log.info("[{}]是否存在[{}]" , key,value);
        // 用法2:添加元素
        client.opsForValue().set(key, "v1", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS );
        // 用法3:获取元素
        String getValue = client.opsForValue().get(key);
        log.info("getValue : [{}]" , getValue);
        // 用法4:计数
        String counter ="counter:key";
        client.opsForValue().set(counter, "0", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
        client.opsForValue().increment(counter);
        client.opsForValue().increment(counter);
        currentNum = client.opsForValue().get(counter);
        log.info("currentNum : [{}]" , currentNum);
        client.opsForValue().decrement(counter);
        currentNum = client.opsForValue().get(counter);
        log.info("currentNum : [{}]" , currentNum);

        if(Objects.nonNull(value)){
            Assert.assertTrue(value);
        }
    }

}
相关推荐
羱滒4 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
程序员Android6 分钟前
MTK多帧拍照流程分析
数据库
不剪发的Tony老师8 分钟前
Apache Doris:一款高性能的实时数据仓库
数据库·数据仓库
2501_9032386518 分钟前
Git Bash:Windows下的强大命令行工具
windows·git·bash·个人开发
甜可儿19 分钟前
redis序列化设置
数据库·redis
GGBondlctrl24 分钟前
【SpringBoot】论坛项目中如何进行实现发布文章,以及更新对应数据库的数据更新
数据库·springboot项目·三层设计思想·文章发布项目
若云止水37 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
jay丿1 小时前
Django模板系统深入
数据库·django·sqlite
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
千层冷面2 小时前
Redis除了做缓存还能做什么?
数据库·redis·缓存