当前位置: 首页 > 知识库问答 >
问题:

在Redis Cli中仍然可见过期的Redisson密钥

祁远
2023-03-14
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bridgelabz</groupId>
    <artifactId>redissonApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>redissonApp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.3.0</version>
        </dependency>

    </dependencies>
</project>
package com.bridgelabz.redissonApp;

public class Token {

    private String accessToken;
    private int id;

    public Token() { }


    public Token(String accessToken, int id) {

        this.accessToken = accessToken;
        this.id = id;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Token [accessToken=" + accessToken + ", id=" + id + "]";
    }   

}
package com.bridgelabz.redissonApp;

import java.util.concurrent.TimeUnit;

import org.redisson.Redisson;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.api.LocalCachedMapOptions.EvictionPolicy;
import org.redisson.config.Config;


public class App {
    public static void main(String[] args) {
        Config config = new Config();

        config.useSingleServer().setAddress("127.0.0.1:6379");

        // LocalCachedMapOptions localCachedMapOptions =
        // LocalCachedMapOptions.defaults()
        // .evictionPolicy(EvictionPolicy.LFU);

        RedissonClient redisson = Redisson.create(config);

        try {

            RMapCache<Integer, Token> map = redisson.getMapCache("TestMap");

            Token myToken = new Token("abc", 1);

            map.put(1, myToken, 10, TimeUnit.SECONDS);

            System.out.println("Stored value with key 1 is: " + map.get(1));

        }

        finally {

            redisson.shutdown();

        }

    }
}
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Stored value with key 1 is: Token [accessToken=abc, id=1]
LF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Stored value with key 1 is: null

但是当我运行redis-cli时,我仍然得到输出中的值:

127.0.0.1:6379> hget TestMap 1
"\x00\x00\x00\x00\x00\x00\x00\x00H\x00\x00\x00\x00\x00\x00\x00{\"@class\":\"com.bridgelabz.redissonApp.Token\",\"accessToken\":\"abc\",\"id\":1}"

为什么redis的钥匙没有被移除?供参考:所有的东西都在我的本地机器上测试,只包括Redis。

共有1个答案

吴高畅
2023-03-14

Redis不支持散列的开箱即用的单个元素驱逐。因此,Redisson构建了自己的解决方案,并将其命名为Mapcache

因此,使用mapcache,您现在有两个级别的过期控制:Redis提供的键级别和Redisson提供的字段级别。

在测试代码中:

RMapCache<Integer, Token> map = redisson.getMapCache("TestMap");

Token myToken = new Token("abc", 1);

map.put(1, myToken, 10, TimeUnit.SECONDS);
 类似资料: