Is DefaultHashOperations and so on thread safe in RedisTemplate?

    public <HK, HV> HashOperations<K, HK, HV> opsForHash() {
        return new DefaultHashOperations(this);
    }

A new DefaultHashOperations is generated every time I use opsForHash ()
if only one DefaultHashOperations is generated in a RedisUtil and this one is called every time, what"s the problem? Are
DefaultHashOperations,DefaultSetOperations and so on thread safe?

@Component
public final class RedisUtil {
    private HashOperations<String, Object, Object> hashOperations; 

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostConstruct
    void init(){
        hashOperations = redisTemplate.opsForHash();
    }

    public Set<Object> allFields(String key) {
        return redisTemplate.opsForHash().keys(key);
    }


    public List<Object> allValues(String key) {
        return redisTemplate.opsForHash().values(key);
    }
}

Jun.11,2022

should be said to be thread-safe, because the data shared by DefaultHashOperations is RedisTemplate, and this member variable is Once configured, this class is thread-safe. , that is, it is thread-safe once configured.

but in fact, the construction of DefaultHashOperations is lightweight, and considering that the extension implementation of RedisTemplate may be different, it is not necessary to do caching, but it is not convenient to extend.

Menu