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

Callbacks are not parameterized fully [DATAREDIS-235] #815

Open
spring-projects-issues opened this issue Aug 19, 2013 · 3 comments
Open
Labels
theme: 4.0 type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

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

@spring-projects-issues
Copy link
Author

Markus Heiden commented

The type parameters K and V should be at type level, not a method level. Otherwise it is not possible to use SessionCallback as a lambda. RedisTemplate#execute should look like:
public <T> T execute(SessionCallback<K, V, T> session) { ... }
This way type safety is ensured.

@spring-projects-issues spring-projects-issues added status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement labels Dec 30, 2020
@zanyoo
Copy link

zanyoo commented Dec 14, 2021

No one assigned

@HongJian-Yang
Copy link

2023.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: 4.0 type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants