Skip to content

How to set TTL on read-through cache #4909

Open

Description

@DanilDelegator

I"m trying to set a Redisson distributed cache with a read-through cache using MapLoader.

There is no apparent way to set the TTL on values that get injected using the MapLoader. Example (from wiki):

        MapLoader<String, String> mapLoader = new MapLoader<String, String>() {
            
            @Override
            public Iterable<String> loadAllKeys() {
                List<String> list = new ArrayList<String>();
                Statement statement = conn.createStatement();
                try {
                    ResultSet result = statement.executeQuery("SELECT id FROM student");
                    while (result.next()) {
                        list.add(result.getString(1));
                    }
                } finally {
                    statement.close();
                }

                return list;
            }
            
            @Override
            public String load(String key) {
                PreparedStatement preparedStatement = conn.prepareStatement("SELECT name FROM student where id = ?");
                try {
                    preparedStatement.setString(1, key);
                    ResultSet result = preparedStatement.executeQuery();
                    if (result.next()) {
                        return result.getString(1);
                    }
                    return null;
                } finally {
                    preparedStatement.close();
                }
            }
        };
MapOptions<K, V> options = MapOptions.<K, V>defaults()
                              .loader(mapLoader);

RMapCache<K, V> map = redisson.getMapCache("test", options);

Now that this is set-up I can use it like this:

println(map["1234"]) // loader loads 1234 into redis
// Let's say the values change in the DB
println(map["1234"]) // loader does not load 1234 as it is already in redis. The value is forever incorrect.

Activity

  1. DanilDelegator commented on Mar 7, 2023

    @DanilDelegator
    Author

    I believe the main problem is that MapOption passed to getMapCache assumes a write-through|behind strategy where a TTL is not a must.

  2. added this to the 3.22.2 milestone on Jun 29, 2023
  3. modified the milestones: 3.23.0, 3.22.x on Jul 10, 2023
  4. tkboon82 commented on Oct 25, 2024

    @tkboon82

    Hi, any workaround for the ability to set ttl when using maploader to load key ? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions