Redis梦幻整合:解锁数据的神秘力量

技术派项目源码地址 :

下载安装Redis

Windwos

Redis - Github.com

image.png

  • 下载msi文件直接打开即可安装
Linux - Docker
  • 这里只介绍在Docker部署Redis, 方便中间件管理

  • 拉取镜像

docker pull redis:7.0.12
  • 拷贝文件
mkdir -p /data/redis/{conf,data,log}
touch /data/redis/log/redis.log

docker run -p 6379:6379 --name redis -d redis:7.0.12

docker cp redis:/data /data/redis
docker cp redis:/etc/redis.log /data/redis/log
docker cp redis:/etc/redis/redis.conf /data/redis/conf

chmod -R 777 /data/redis/conf/redis.conf /data/redis/log/redis.log /data/redis/data

docker rm -f redis
  • 创建容器
docker run --name redis \
-p 6379:6379 --restart=always \
-v /data/redis/data:/data \
-v /data/redis/log/redis.log:/etc/redis.log \
-v /data/redis/conf:/etc/redis/redis.conf \
-d redis:7.0.12 redis-server /etc/redis/redis.conf

整合Redis

引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
注入使用
  • 当我们使用默认的 127.0.0.1, 端口号 6379,无密码时,无需在配置文件中做任何其他的操作,
  • 直接在需要的地方,引入 RestTemplate 就可以直接使用了, 如下
@SpringBootApplication
public class Application {

    public Application(RedisTemplate<String, String> redisTemplate) {
        // 往Redis中写入key=hello, value=world
        redisTemplate.opsForValue().set("hello", "world");
        // 查询redis中key=hello的值
        String ans = redisTemplate.opsForValue().get("hello");
        Assert.isTrue("world".equals(ans));
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

自定义参数配置

image.png

  • 也可以配置连接池相关配置

image.png

  • 记得在 **pom.xml** 添加连接池依赖
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

Redis集群

  • 在实际的生产环境中,redis通常是以集群的方式提供服务的,
  • 因此当我们的服务需要连接的是redis集群时,可以如下配置
spring:
  redis:
    password:
    cluster:
      nodes: 192.168.0.203:7000,192.168.0.203:7001,192.168.0.203:7002
      max-redirects: 3
    lettuce:
      pool:
        max-idle: 16
        max-active: 32
        min-idle: 8

**搭建Redis集群的时候需要注意 : **

  • 注释bind 127.0.0.1,允许其他机器访问
  • 在创建集群时,使用局域网ip而不是127.0.0.1

Redis操作封装类RedisClient

我们封装了一个基于RedisTemplate的RedisClient工具类,再项目启动之时,主动注入RestTemplate
image.png

public class RedisClient {
    private static final Charset CODE = StandardCharsets.UTF_8;
    private static final String KEY_PREFIX = "pai_";
    private static RedisTemplate<String, String> template;

    public static void register(RedisTemplate<String, String> template) {
        RedisClient.template = template;
    }

    public static void nullCheck(Object... args) {
        for (Object obj : args) {
            if (obj == null) {
                throw new IllegalArgumentException("redis argument can not be null!");
            }
        }
    }

    /**
     * 技术派的缓存值序列化处理
     *
     * @param val
     * @param <T>
     * @return
     */
    public static <T> byte[] valBytes(T val) {
        if (val instanceof String) {
            return ((String) val).getBytes(CODE);
        } else {
            return JsonUtil.toStr(val).getBytes(CODE);
        }
    }

    /**
     * 生成技术派的缓存key
     *
     * @param key
     * @return
     */
    public static byte[] keyBytes(String key) {
        nullCheck(key);
        key = KEY_PREFIX + key;
        return key.getBytes(CODE);
    }

    /**
     * 查询缓存
     *
     * @param key
     * @return
     */
    public static String getStr(String key) {
        return template.execute((RedisCallback<String>) con -> {
            byte[] val = con.get(keyBytes(key));
            return val == null ? null : new String(val);
        });
    }

    /**
     * 设置缓存
     *
     * @param key
     * @param value
     */
    public static void setStr(String key, String value) {
        template.execute((RedisCallback<Void>) con -> {
            con.set(keyBytes(key), valBytes(value));
            return null;
        });
    }

    /**
     * 删除缓存
     *
     * @param key
     */
    public static void del(String key) {
        template.execute((RedisCallback<Long>) con -> con.del(keyBytes(key)));
    }
}
  • 22
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值