Redis——配置与使用

服务器配置Redis

安装redis:

yum -y install redis

后台启动redis:

redis-server /etc/redis.conf &

启动redis客户端:

redis-cli

正常使用如下:
在这里插入图片描述
设置远程连接

  1. 打开redis配置文件 /etc/redis.conf
  2. 注释掉 bind 127.0.0.1
  3. 将protected-mode yes 改为protected-mode no
  4. 使用redis-cli shutdown关闭redis
  5. 使用redis-server /etc/redis.conf & 重启redis

SpringBoot配置Redis

添加相关依赖
在这里插入图片描述
配置文件中配置redis

spring.redis.database=15
spring.redis.port=6379
spring.redis.host=服务器地址

手动操作Redis

属性注入StringRedisTemplate

@Autowired
private StringRedisTemplate stringRedisTemplate;

存储数据到redis:

@RequestMapping("url路径")
public String 方法名(){
    stringRedisTemplate.opsForValue().set(key, value);
    return 返回值;
}

获取redis中的数据

@RequestMapping("url路径")
public String 方法名(){
    String result = stringRedisTemplate.opsForValue().get(key);
    return 返回值;
}

例如:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/set")
    public String setRedis(){
        stringRedisTemplate.opsForValue().set("username", "zhangsan");
        return "redis存储成功!";
    }

    @RequestMapping("/get")
    public String getRedis(){
        String result = stringRedisTemplate.opsForValue().get("username");
        return result;
    }
}

访问127.0.0.1:8080/set存储redis
在这里插入图片描述
访问127.0.0.1:8080/get获取数据
在这里插入图片描述

使用注解操作Redis

首先在启动类中添加下面这个注解

@EnableCaching

添加数据:

@RequestMapping("url路径")
@Cacheable(value = "spring.cache", key = "#key")
public String set(String key, String value){
   if(!StringUtils.hasLength(key) || !StringUtils.hasLength(value)){
       return null;
   }
   return "value";
}

更新数据:

@CachePut(value = "spring.cache", key = "key")
@RequestMapping("url路径")
public String put(String key, String value){
    if(!StringUtils.hasLength(key) || !StringUtils.hasLength(value)){
        return null;
    }
    return "value";
}

删除数据:

@RequestMapping("url路径")
@CacheEvict(value = "spring.cache", key = "#key")
public void del(String key, String value){
    System.out.println("缓存删除");
}

例如:

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisAnnController {
    @RequestMapping("/ann-set")
    @Cacheable(value = "spring.cache", key = "#name+'-'+#pnumber")
    public String set(String name, String pnumber){
       if(!StringUtils.hasLength(name) || !StringUtils.hasLength(pnumber)){
           return null;
       }
       return "name=" + name + "|pnumber=" + pnumber;
    }

    @CachePut(value = "spring.cache", key = "#name+'-'+#pnumber")
    @RequestMapping("/ann-put")
    public String put(String name, String pnumber){
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(pnumber)){
            return null;
        }
        return "[name=" + name + " <-> pnumber=" + pnumber + "]";
    }

    @RequestMapping("/ann-del")
    @CacheEvict(value = "spring.cache", key = "#name+'-'+#pnumber")
    public void del(String name, String pnumber){
        System.out.println("缓存删除");
    }
}

访问127.0.0.1:8080/ann-set?数据 设置缓存
在这里插入图片描述
访问127.0.0.1:8080/ann-put?数据 更新缓存
在这里插入图片描述
访问127.0.0.1:8080/ann-del?数据 删除缓存
在这里插入图片描述

session存储到redis

添加依赖
在这里插入图片描述
修改配置文件

spring.session.store-type=redis
server.servlet.session.timeout=1800
spring.session.redis.flush-mode=on_save
spring.session.redis.namespace=spring:session
spring.redis.host=服务器地址
spring.redis.port=6379

然后正常存储和读取session即可

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    private final static String SESSION_KEY = "USER_SESSION_KEY";

    @RequestMapping("/login")
    public String login(HttpSession session){
        session.setAttribute(SESSION_KEY, "zhangsan");
        return "login success";
    }

    @RequestMapping("/get")
    public String login(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null){
            String username = (String) session.getAttribute(SESSION_KEY);
            if(username != null){
                return username;
            }
        }
        return "未登陆!";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值