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

具體使用方法 可以詳查 五種類型操作方法集合

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