Springboot and Redis integrate simple Redis tool class implementation

Recently, the project needs to deal with a business with a large amount of data. Under consideration, high-frequency access/reading decided to use Redis. I studied Redis for two days under my own Springboot framework, and summarized the results.

Introduction to the development environment

  • JDK1.7
  • Redis

base dependency

 <dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.1</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Redis configuration

The application.yml that omits part of the content is the default configuration, and it is optional. At the same time, you can also configure the attributes such as the maximum connection time-out time here, you can consider referring to other online blogs

server:
    redis:
        host: localhost  # 162上面的Redis
        port: 6379
        password: 

Create RedisConfig to configure some custom content. My configuration is relatively simple, only a RedisTemplate that serializes data according to JDK is registered

@Configuration
public class RedisConfig extends CachingConfigurerSupport{
    /**
     * 注入 RedisConnectionFactory
     */
    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    /**
     * 实例化 RedisTemplate 对象
     * Primary 在autoware时优先选用我注册的bean. 
     * 因为在redis框架中有注册一个StringRedisTemplate,避免注入冲突
     * @return
     */
    @Bean
    @Primary
    public RedisTemplate<String, Object> functionDomainRedisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
        return redisTemplate;
    }

    /**
     * 设置数据存入 redis 的序列化方式
     *
     * @param redisTemplate
     * @param factory
     */
    private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
    }
}

Springboot advocates out-of-the-box use, and this Redis dependency is in line with this concept. In fact, even if you do not do any configuration, you can still connect normally.

Finally, add @EnableCaching on the springboot main class to enable cache management, this cannot be omitted

@EnableCaching
class Application {

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

Redis helper class

According to the layered idea, the operation of the Redis database belongs to the Dao layer, and the data should not be directly operated in the Service, and I am not sure whether Redis has an ORM framework, so in order to facilitate the operation of the Redis database, the RedisDBHelper is encapsulated for easy operation, and the interface is first

/**
 * K 指以hash结构操作时 键类型
 * T 为数据实体 应实现序列化接口,并定义serialVersionUID
 * RedisTemplate 提供了五种数据结构操作类型 hash / list / set / zset / value
 * 方法命名格式为 数据操作类型 + 操作 如 hashPut 指以hash结构(也就是map)想key添加键值对
 * Created by hp on 2018/3/1.
 */
public interface RedisDBHelper<HK, T> {
    /**
     * Hash结构 添加元素
     * @param key key
     * @param hashKey hashKey
     * @param domain 元素
     */
    void hashPut(String key, HK hashKey,T domain);

    /**
     * Hash结构 获取指定key所有键值对
     * @param key
     * @return
     */
    Map< HK, T> hashFindAll(String key);

    /**
     * Hash结构 获取单个元素
     * @param key
     * @param hashKey
     * @return
     */
    T hashGet(String key,HK hashKey);

    void hashRemove(String key, HK hashKey);

    /**
     * List结构 向尾部(Right)添加元素
     * @param key
     * @param domain
     * @return
     */
    Long listPush(String key, T domain);

    /**
     * List结构 向头部(Left)添加元素
     * @param key
     * @param domain
     * @return
     */
    Long listUnshift(String key, T domain);

    /**
     * List结构 获取所有元素
     * @param key
     * @return
     */
    List<T> listFindAll(String key);

    /**
     * List结构 移除并获取数组第一个元素
     * @param key
     * @return
     */
    T listLPop(String key);

    void remove(String key);

/**
     * 设置过期时间
     * @param key 键
     * @param timeout 时间
     * @param timeUnit 时间单位
     */
    boolean expirse(String key, long timeout, TimeUnit timeUnit);
}

Here I only add a few interfaces according to my current business needs. If there are other needs, I can add and implement them myself. I provide a simple implementation of this interface.

/**
 * 接口的简单实现
 * Created by hp on 2018/3/1.
 */
@Service("RedisDBHelper")
public class RedisDBHelperImpl<HK, T> implements RedisDBHelper<HK, T>{

    // 在构造器中获取redisTemplate实例, key(not hashKey) 默认使用String类型
    private RedisTemplate<String, T> redisTemplate;

    // 在构造器中通过redisTemplate的工厂方法实例化操作对象
    private HashOperations<String, HK, T> hashOperations;

    private ListOperations<String, T> listOperations;

    private ZSetOperations<String, T> zSetOperations;

    private SetOperations<String, T> setOperations;

    private ValueOperations<String, T> valueOperations;

    // IDEA虽然报错,但是依然可以注入成功, 实例化操作对象后就可以直接调用方法操作Redis数据库
    @Autowired
    public RedisDBHelperImpl (RedisTemplate<String, T>  redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.hashOperations = redisTemplate.opsForHash();
        this.listOperations = redisTemplate.opsForList();
        this.zSetOperations = redisTemplate.opsForZSet();
        this.setOperations = redisTemplate.opsForSet();
        this.valueOperations = redisTemplate.opsForValue();
    }


    @Override
    public void hashPut(String key, HK hashKey, T domain) {
        hashOperations.put(key, hashKey, domain);
    }

    @Override
    public Map<HK, T> hashFindAll(String key) {
        return hashOperations.entries(key);
    }

    @Override
    public T hashGet(String key, HK hashKey) {
        return hashOperations.get(key, hashKey);
    }

    @Override
    public void hashRemove(String key, HK hashKey) {
        hashOperations.delete(key, hashKey);
    }

    @Override
    public Long listPush(String key, T domain) {
        return listOperations.rightPush(key, domain);
    }

    @Override
    public Long listUnshift(String key, T domain) {
        return listOperations.leftPush(key, domain);
    }

    @Override
    public List<T> listFindAll(String key) {
        if (! redisTemplate.hasKey(key)){
            return null;
        }
        return listOperations.range(key,0, listOperations.size(key));
    }

    @Override
    public T listLPop(String key) {
        return listOperations.leftPop(key);
    }

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

    @Override
    public boolean expire(String key, long timeout, TimeUnit timeUnit) {
        return redisTemplate.expire(key, timeout, timeUnit);
    }
}

On the whole, it is a simple encapsulation of Operations, so that you can use it more easily. So how should RedisDBHelper be used? First define a simple entity

public class Person implements Serializable {
    // 为了能够正确得序列化和反序列化,这个属性必不可少
    private static final long serialVersionUID = - 1L;

    private String id;

    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

PersonServiceImpl super simple example

The way to use it is to directly inject autoware into the instance of RedisDBHelper, and define the generic type as required.

@Service
public class PersonServiceImpl implements PersonService{
    @Autowired
    RedisDBHelper<String, Person> redisDBHelper;

    // 你可以理解为这个是表名
    private static final String KEY= "PERSON_KEY";

    /**
     * 储存一个Person到一个Map结构中
     * @param person
     */
    public void putPerson(Person person) {
        redisDBHelper.hashPut(KEY, person.getId(), person);
    }
}

Because the above codes are all excerpted from the project code, so there is no strict test. In the project, there is basically no problem in saving and deleting 50 data per second. Because it is another remote redis server, there is occasional Read time out. If If you find any problems, please feedback and exchange, thank you!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324393452&siteId=291194637