spring-projects / spring-projects/spring-data-redis

Callbacks are not parameterized fully [DATAREDIS-235]

Open
#815 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

theme: 4.0 type: enhancement
Dominant language
Java
Stars
1.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

William Hoyle opened DATAREDIS-235 and commented

Template callbacks, for example SessionCallback, only have return type parameters. This leads to some unnecessary casts. Also, the callback methods have type parameters with names that shadow the template parameter names.

Consider:

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.hash.HashMapper;

/**
 * Utility for transactionally mapping objects to/from hashes.
 */

public class Hasher<T, K, V, HK, HV> {
    private RedisTemplate<K, V> template;
    private HashMapper<T, HK, HV> mapper;

    public Hasher(RedisTemplate<K, V> template, HashMapper<T, HK, HV> mapper) {
        this.template = template;
        this.mapper = mapper;
    }

    public T get(K key) {
        HashOperations<K, HK, HV> hashOps = template.opsForHash();
        return mapper.fromHash(hashOps.entries(key));
    }

    public void put(final K key, final T value) {

        //
        // Clear hash and set in a transaction
        //

        template.execute(new SessionCallback<Void>() {
            @Override
            public <K, V> Void execute(RedisOperations<K, V> ops) throws DataAccessException {
                ops.multi();
                // Clear all values.
                // Need to cast because SessionCallback is not <T, K, V>.
                ops.delete((K) key); // execute<K> shadows Hasher<K>
                HashOperations<K, HK, HV> hashOps = ops.opsForHash();
                // Put all values.
                // Need to cast again.
                hashOps.putAll((K) key, mapper.toHash(value)); // cast again
                ops.exec();
                return null;
            }
        });
    }
}

1 votes, 2 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names SessionCallback, RedisOperations, RedisTemplate, HashOperations, and HashMapper; start by locating these callback interfaces and their generic declarations. Compare related template callbacks and their usages for unnecessary casts and shadowed type parameters. Done means callback input types are fully parameterized consistently and the example no longer needs casts; verify with the existing project tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, redis, spring
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.