Skip to content

RedisForWindows 安装与使用

Redis 官方并不直接支持 Windows,但微软提供了一个 Windows 版本的 Redis,可以通过去官网获取,值得注意的是,Redis for Windows 是一个较旧的版本,不包含最新的功能和优化。如果你需要最新的 Redis 功能,建议在 Linux 或 WSL(Windows Subsystem for Linux)上运行 Redis。

安装

官网 下载 Redis-x64-3.0.504.zip,之后选择目录进行解压。例如,解压为 D:\software\Redis-x64-3.0.504

使用

启动 redis-server

shell
.\redis-server.exe .\redis.windows.conf

SpringBoot 使用 Redis

依赖

xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置

text
spring.data.redis.host=localhost
spring.data.redis.port=6379

Java 调用 - application

java
package cn.aileading.site.java.application;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 网站计数器
 */
@Service
public class ViewCounterService {
    private static final String SITE_CACHE_KEY = "site:pv";
    private static final String PAGE_CACHE_KEY_PREFIX = "site:pv:";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 单个页面浏览量 +1(返回最新值)
     */
    public Long incrementPageView(String pageId) {
        return stringRedisTemplate.opsForValue().increment(PAGE_CACHE_KEY_PREFIX + pageId);
    }

    /**
     * 网站总浏览量 +1(返回最新值)
     */
    public Long incrementSiteView() {
        return stringRedisTemplate.opsForValue().increment(SITE_CACHE_KEY);
    }

    /**
     * 获取单个页面浏览量
     */
    public Long getPageViews(String pageId) {
        String value = stringRedisTemplate.opsForValue().get(PAGE_CACHE_KEY_PREFIX + pageId);
        return value != null ? Long.parseLong(value) : 0L;
    }

    /**
     * 获取网站总浏览量
     */
    public Long getSiteViews() {
        String value = stringRedisTemplate.opsForValue().get(SITE_CACHE_KEY);
        return value != null ? Long.parseLong(value) : 0L;
    }
}

Java 调用 - interfaces

java
package cn.aileading.site.java.interfaces;

import cn.aileading.site.java.application.ViewCounterService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/views")
public class ViewController {
    @Resource
    private ViewCounterService counterService;

    /**
     * 访问单个页面(自动计数)
     */
    @GetMapping("/page/addView")
    public Boolean trackPageView(@RequestParam String pageId) {
        Long pageViews = counterService.incrementPageView(pageId);
        Long siteViews = counterService.incrementSiteView();
        return true;
    }

    /**
     * 查询单个页面的浏览量
     */
    @GetMapping("/getPageViews")
    public Long getPageViews(@RequestParam String pageId) {
        return  counterService.getPageViews(pageId);
    }

    /**
     * 查询网站整体的浏览量
     */
    @GetMapping("/getSiteViews")
    public Long getSiteViews() {
        return  counterService.getSiteViews();
    }
}

测试

浏览器输入 http://localhost:8080/views/page/addView?pageId=docs/start/install-env.html 测试写 redis;

输入 http://localhost:8080/views/getPageViews?pageId=docs/start/install-env.html 测试读 redis