SpringBoot整合Redis

SpringBoot整合Redis
第一步:创建项目
勾选:web,freemarker,Redis
SpringBoot整合Redis_第1张图片
SpringBoot整合Redis_第2张图片

SpringBoot整合Redis_第3张图片
只测redis不要勾选Mybatis,MySQL,若选择了MySQL,启动的时候会加载DB数据源,所以需要配置,不配置启动会报错。

第二步:配置redis
配置application.properties
SpringBoot整合Redis_第4张图片
可以使用远程链接Linux修改配置文件。
扩展集群配置:
集群的只需要开启下面两项:

#集群节点host:port,多个节点逗号分隔 spring.redis.cluster.nodes=192.168.48.28:7000,192.168.48.28:7001,192.168.48.28:7002 #在群集中执行命令时要遵循的最大重定向数目。 spring.redis.cluster.max-redirects=5

第三步:在入口启动类添加缓存支持
添加cache配置类
@EnableCaching
SpringBoot整合Redis_第5张图片

第四步:开发redis服务层
自动注入与使用
1)RedisTemplate
Key自动生成前缀(相对安全)
可以存储封装类型
存储对象需要序列化UserDTO implements Serializable
2)StringRedisTemplate
仅存储字符串类型(很多项目也只用字符串,json字符串)
Key保持不变(方便服务端查看确认)
这两个类只是在整合SpringBoot才有。

package com.dragon.service;

public interface MyRedis {

    //存储
    public void set(String key, String value);

    //存储的时候就设置过期时间
    public void set(String key, String value, int timeout);

    //取值
    public String get(String key);

    //删除
    public void del(String key);

    //递增
    public Long incr(String key);

    //递减
    public Long decr(String key);

    //过期时间
    public void expire(String key, int second);

    //得到剩余时间
    public Long getExp(String key);

    //队列的存与取:先进先去
    public void lpush(String key, Object value);

    public Object rpop(String key);

    public void hset(String key, String item, String value);

    public String hget(String key, String item);

    public boolean exists(String key);

}

package com.dragon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
public class MyRedisImpl implements MyRedis {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value);
    }

    @Override
    public void set(String key, String value, int timeout) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value, timeout);
    }

    @Override
    public String get(String key) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String o = (String) valueOperations.get(key);
        return o;
    }

    @Override
    public void del(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public Long incr(String key) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Long increment = valueOperations.increment(key, 1);
        return increment;
    }

    @Override
    public Long decr(String key) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Long increment = valueOperations.increment(key, -1);
        return increment;
    }

    @Override
    public void expire(String key, int second) {
        redisTemplate.expire(key, second, TimeUnit.SECONDS);
    }

    @Override
    public Long getExp(String key) {
        Long expire = redisTemplate.getExpire(key);
        return expire;
    }

    @Override
    public void lpush(String key, Object value) {
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPush(key, value);
    }

    @Override
    public Object rpop(String key) {
        ListOperations listOperations = redisTemplate.opsForList();
        Object o = listOperations.rightPop(key);
        return o;
    }

    @Override
    public void hset(String key, String item, String value) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put(key, item, value);
    }

    @Override
    public String hget(String key, String item) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        String o = (String) hashOperations.get(key, item);
        return o;
    }

    @Override
    public boolean exists(String key) {
        Boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            return true;
        }
        return false;
    }
}

注意和Jedis区别.
Hash类型:添加是put方法
SpringBoot整合Redis_第6张图片
Hash删除:
SpringBoot整合Redis_第7张图片
本质仍然是Jedis
ctrl+t看底层源码仍然是jedis
对外接口:
SpringBoot整合Redis_第8张图片
底层:
SpringBoot整合Redis_第9张图片
第五步:开发Action
没有选用的功能,不要勾选,springboot框架会自动加载。

package com.dragon.action;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.dragon.service.MyRedis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @Date 2018/12/10 16:04
 * @Classname SmsAction
 */
@Controller
public class SmsAction {
    private static final Logger logger = LoggerFactory.getLogger(PlaceholderUtils.class);

    @Autowired
    private MyRedis myRedis;

    @Autowired
    private RecordService recordService;

    //跳转模板页面
    @RequestMapping("/show")
    public String showUI() {
        //逻辑视图
        return "mtlogin";
    }

    @RequestMapping("/set")
    @ResponseBody
    public Map setKey() {
        Map map = new HashMap<>();
        myRedis.set("java", "好程序员");
        map.put("data", "ok");
        return map;
    }

    @RequestMapping("/get")
    @ResponseBody
    public Map getKey() {
        Map map = new HashMap<>();
        String java = myRedis.get("java");
        map.put("code", "ok");
        Map map2 = new HashMap<>();
        map2.put("java", java);
        map.put("data", map2); //这里存储还是json
        return map; //嵌套json
    }
   }

第六步:测试
RedisTemplate模板的形式:
Key自动加前缀
SpringBoot整合Redis_第10张图片

SpringBoot整合Redis_第11张图片

你可能感兴趣的:(SpringBoot整合Redis)