Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opsForHash() should reuse cached instances #2970

Closed
HK-hub opened this issue Aug 19, 2024 · 1 comment
Closed

opsForHash() should reuse cached instances #2970

HK-hub opened this issue Aug 19, 2024 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@HK-hub
Copy link

HK-hub commented Aug 19, 2024

Curious question

Why in the series of OpsFor* methods of RedisTemplate, opsForHash() always returns a new instance object, valueOps, listOps, setOps, zSetOps and other data structures return final modified instantiated property objects. Is there a particular reason for this

dependency

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

opsForHash() Method

this is opsForHash() method body for 3.2.2 version

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

and the next code is some opsFor* that return the singleton object

    private final ValueOperations<K, V> valueOps = new DefaultValueOperations(this);
    private final ListOperations<K, V> listOps = new DefaultListOperations(this);
    private final SetOperations<K, V> setOps = new DefaultSetOperations(this);
    private final StreamOperations<K, ?, ?> streamOps = new DefaultStreamOperations(this, ObjectHashMapper.getSharedInstance());
    private final ZSetOperations<K, V> zSetOps = new DefaultZSetOperations(this);
    private final GeoOperations<K, V> geoOps = new DefaultGeoOperations(this);
    private final HyperLogLogOperations<K, V> hllOps = new DefaultHyperLogLogOperations(this);
    private final ClusterOperations<K, V> clusterOps = new DefaultClusterOperations(this);

I would like to know why the design is like this, I searched for related questions but found no discussion about it

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 19, 2024
@mp911de mp911de self-assigned this Aug 19, 2024
@mp911de mp911de changed the title why opsForHash() method always returns a new instance object Why does opsForHash() returns a new instance? Sep 17, 2024
@mp911de mp911de changed the title Why does opsForHash() returns a new instance? opsForHash() should reuse cached instances Sep 17, 2024
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 17, 2024
@mp911de
Copy link
Member

mp911de commented Sep 17, 2024

This behavior has existed since the earliest days of hash operations (Early 2010). I cannot tell why this is, though I assume that the code didn't want to mess with generic assignability. Its reactive variant, ReactiveRedisTemplate.opsForHash() uses a cached variant. Tests pass when we change the object to a cached variant so there's no reason why it needs to remain this way.

mp911de added a commit that referenced this issue Sep 17, 2024
Reword Javadoc comments for clarity and add missing `@SuppressWarnings` annotations.

See #2970
mp911de added a commit that referenced this issue Sep 17, 2024
Reword Javadoc comments for clarity and add missing `@SuppressWarnings` annotations.

See #2970
@mp911de mp911de added this to the 3.3.5 (2024.0.5) milestone Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants