【Redis系列1】初探Redis

马里奥 2020年09月17日 17次浏览

前言

这个系列记录我学习、使用到的Redis知识点。

什么是Redis

redis是一种支持Key-Value等多种数据结构的存储系统。可用于缓存,事件发布或订阅,高速队列等场景。该数据库使用ANSI C语言编写,支持网络,提供字符串,哈希,列表,队列,集合结构直接存取,基于内存,可持久化。

安装

Redis安装与RedisDesktopManager工具使用

SpringBoot中集成Redis

有两种方式:jedis和redistemplate,本文采用前者。

  • 添加依赖
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>
  • 创建配置类读取配置
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * redis配置类
 */

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {
    private String host;
    private Integer port;
    private String password;
    private Integer timeout;
    private Integer poolMaxTotal;
    private Integer poolMaxIdle;
    private Integer poolMaxWait;
}

ConfigurationProperties注解默认读取的是classpath下的application.properties,redis的配置可以参考以下:

# redis
redis.host=127.0.0.1
redis.port=6379
#redis.password=
# 超时时间 s
redis.timeout= 100
# 连接池最大连接数
redis.poolMaxTotal=1000
# 连接池最大空闲连接
redis.poolMaxIdle=500
# 连接池最大等待时间 s
redis.poolMaxWait=500
  • 采用工厂类统一生产redis连接,并将连接纳入spring容器管理
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
public class RedisPoolFactory {

    @Autowired
    private RedisConfig redisConfig = null;

    @Bean //指示返回一个Spring容器管理的Bean
    public JedisPool JedisPoolFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);
        JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
                redisConfig.getTimeout() * 1000, redisConfig.getPassword(), 0);
        return jp;
    }
}

通常的,一个JedisPool实例代表一个redis连接池。

  • 使用JedisPool

接下来可以通过自动注入使用redis了。

@Autowired
private JedisPool jedisPool;
...
Jedis jedis = jedisPool.getResource(); //获取redis连接
jedis.set(key, value);
...

为了提高代码复用,将redis的存取等操作封装成服务。

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {
    @Autowired
    private JedisPool jedisPool;

    public <T> boolean set(KeyPrefix prefix, String key, T value) {
        return set(prefix, key, value, 0);
    }

    public <T> boolean set(KeyPrefix prefix, String key, T value, int exTime) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String str = beanToString(value);
            if (str == null || str.length() <= 0) {
                return false;
            }
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            if (exTime == 0) {
                //直接保存
                jedis.set(realKey, str);
            } else {
                //设置过期时间
                jedis.setex(realKey, exTime, str);
            }
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    public <T> T get(KeyPrefix prefix, String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            String str = jedis.get(realKey);
            T t = stringToBean(str, clazz);
            return t;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置过期时间
     */
    public Long expice(KeyPrefix prefix, String key, int exTime) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.expire(prefix.getPrefix() + key, exTime);
            return result;
        } finally {
            returnToPool(jedis);
        }
    }

    public Long del(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.del(prefix.getPrefix() + key);
            return result;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断key是否存在
     */
    public <T> boolean exists(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            return jedis.exists(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 增加值
     */
    public <T> Long incr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            return jedis.incr(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 减少值
     */
    public <T> Long decr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            return jedis.decr(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    private <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == long.class || clazz == Long.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else {
            return JSON.toJSONString(value);
        }
    }

    private <T> T stringToBean(String str, Class<T> clazz) {
        if (str == null || str.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(str);
        } else if (clazz == String.class) {
            return (T) str;
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(str);
        } else {
            return JSON.toJavaObject(JSON.parseObject(str), clazz);
        }
    }

    private void returnToPool(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}

当体量够大时,键的管理不可忽视:想在redis中添加记录得提供键,键得提前注册。
KeyPrefix.java

public interface KeyPrefix {
    public String getPrefix();
}

AbstractKeyPrefix.java

public abstract class AbstractKeyPrefix implements KeyPrefix {
    private String prefix;

    public AbstractKeyPrefix(String prefix) {
        this.prefix = prefix;
    }

    @Override
    public String getPrefix() {
        String className = getClass().getSimpleName();
        return className + ":" + prefix;
    }
}

注册Item键
ItemKeyPrefix.java

public class ItemKeyPrefix extends AbstractKeyPrefix {
    private ItemKeyPrefix(String prefix) {
        super(prefix);
    }

    public static ItemKeyPrefix item = new ItemKeyPrefix("item");
}

使用redis:
image.png

参考

高性能高并发商品秒杀系统设计与优化