markdown Redis Template配置和使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Redis Template配置和使用相关的知识,希望对你有一定的参考价值。

**Maven**
```xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency><!--spring2.0集成redis所需common-pool2-->
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
	<version>2.4.2</version>
</dependency>
```
## 数据源配置
多数据源配置可参考[SpringBoot2 集成redis多数据源](https://blog.csdn.net/liubenlong007/article/details/86477692),单数据源配置方式如下:
```java
@Configuration
public class RedisConfig {
    @Bean
    public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
//注入redisTemplate
@Autowired
private RedisTemplate<String, Object> redisTemplate;
```
## 无数据结构
```java
/**
 * 获取普通value
 * @param key
 * @return
 */
public Object get(String key) {
    return key == null ? null : redisTemplate.opsForValue().get(key);
}

/**
 * 插入普通的value
 * @param key
 * @param value
 * @return
 */
public boolean set(String key, Object value) {
    try {
        redisTemplate.opsForValue().set(key, value);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

/**
 * 递增num
 * @param key
 * @param num
 * @return
 */
public long incr(String key, long num) {
    return redisTemplate.opsForValue().increment(key, num);
}

/**
 * 递减num
 * @param key
 * @param num
 * @return
 */
public long decr(String key, long num) {
    return redisTemplate.opsForValue().increment(key, -num);
}
```
## Hash表
```java
/**
 * 获取hash表中的item对应value
 * @param key
 * @param item
 * @return
 */
public Object hGet(String key, String item) {
    return redisTemplate.opsForHash().get(key, item);
}

/**
 * 获取key对应的hashMap
 * @param key
 * @return
 */
public Map<Object, Object> hMget(String key) {
    return redisTemplate.opsForHash().entries(key);
}

/**
 * 向hash表中插入数据,如果不存在将创建
 * @param key
 * @param item
 * @param value
 * @return
 */
public boolean hSet(String key, String item, Object value) {
    try {
        redisTemplate.opsForHash().put(key, item, value);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

/**
 * 删除hash表中的值
 * @param key
 * @param item
 */
public void hDel(String key, String item) {
    redisTemplate.opsForHash().delete(key, item);
}

/**
 * 判断hash表中是否有该项的值
 * @param key
 * @param item
 * @return
 */
public boolean hHasKey(String key, String item) {
    return redisTemplate.opsForHash().hasKey(key, item);
}

/**
 * hash表的值原子增加num
 * @param key
 * @param item
 * @param num
 * @return
 */
public double hIncr(String key, String item, long num) {
    return redisTemplate.opsForHash().increment(key, item, num);
}

/**
 * hash表的值原子减少num
 * @param key
 * @param item
 * @param num
 * @return
 */
public double hDecr(String key, String item, long num) {
    return redisTemplate.opsForHash().increment(key, item, -num);
}
```
## 有序集合
```java
/**
 * 添加元素,有序集合是按照元素的score值由小到大排列
 * @param key
 * @param value
 * @param score
 * @return
 */
public Boolean zAdd(String key, String value, double score) {
    return redisTemplate.opsForZSet().add(key, value, score);
}

/**
 * 删除元素
 * @param key
 * @param value
 * @return
 */
public Long zRem(String key, String value) {
    return redisTemplate.opsForZSet().remove(key, value);
}


/**
 * 根据score范围获取集合
 * @param key
 * @param min
 * @param max
 * @return
 */
public Set<Object> zRangeByScore(String key, double min, double max) {
    return redisTemplate.opsForZSet().rangeByScore(key, min, max);
}
```
## 参考
[whvcse/RedisUtil: 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!](https://github.com/whvcse/RedisUtil)   
[SpringBoot整合Redis及Redis工具类撰写](https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html)   
[如何使用RedisTemplate访问Redis数据结构](https://www.jianshu.com/p/7bf5dc61ca06)   

以上是关于markdown Redis Template配置和使用的主要内容,如果未能解决你的问题,请参考以下文章

markdown Acesso ao DOM e ao Template com ViewChild

Spring之Redis访问(Spring-data-redis)

markdown [redis] #redis

markdown [redis常用命令] #bash #redis

[Tools] Batch Create Markdown Files from a Template with Node.js and Mustache

markdown pandoc booktabs minimal template(仅供说明之用)(参见:https://github.com/jgm/pandoc/pull/1076)