Redis总结2

  1. Redis使用类
    stringRedisTemplate
    redisTemplate

    两者关系:
    两个类都处在 org.springframework.data.redis.core 下面

    StringRedisTemplate 继承 RedisTemplate, 将序列话方式更改为 RedisSerializer字符串方式,默认的是JDK序列化方式

public class StringRedisTemplate extends RedisTemplate<String, String> {

	/**
	 * Constructs a new <code>StringRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)}
	 * and {@link #afterPropertiesSet()} still need to be called.
	 */
	public StringRedisTemplate() {
		setKeySerializer(RedisSerializer.string());
		setValueSerializer(RedisSerializer.string());
		setHashKeySerializer(RedisSerializer.string());
		setHashValueSerializer(RedisSerializer.string());
	}

	/**
	 * Constructs a new <code>StringRedisTemplate</code> instance ready to be used.
	 *
	 * @param connectionFactory connection factory for creating new connections
	 */
	public StringRedisTemplate(RedisConnectionFactory connectionFactory) {
		this();
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}
}

在这里插入图片描述
在存储String时,推荐使用StringRedisTemplate

两个类数据不互通

        redisTemplate.boundValueOps("Stringkey").set("sss");
        stringRedisTemplate.opsForValue().set("Stringkey","ssss",5);

操作结果
在这里插入图片描述

2. Redis 操作方法

实现RedisOperations 接口

public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperations<K, V>, BeanClassLoaderAware

接口的全部方法
在这里插入图片描述在这里插入图片描述在这里插入图片描述
开发涉及到对数据的操作

redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
数据结构 操作函数 1 操作函数2
String opsForValue boundValueOps
List opsForList boundListOps
Hash opsForHash boundHashOps
Set opsForSet boundSetOps
Zset opsForZSet boundZSetOps

具体使用方法 可以详查 五种类型操作方法集合

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章