Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用基于用户登录的缓存密钥_Java_Spring Boot_Spring Security_Ehcache - Fatal编程技术网

Java 使用基于用户登录的缓存密钥

Java 使用基于用户登录的缓存密钥,java,spring-boot,spring-security,ehcache,Java,Spring Boot,Spring Security,Ehcache,我有一个SpringBoot+Security+EhCache项目。SecurityContextHolder周围有一个包装类(LoggedUserInfo)和一个处理用户信息的服务: public LoggedUserInfo { public static String getLogin() { return SecurityContextHolder.getContext().getAuthentication().getName(); } } publi

我有一个SpringBoot+Security+EhCache项目。SecurityContextHolder周围有一个包装类(LoggedUserInfo)和一个处理用户信息的服务:

public LoggedUserInfo {
    public static String getLogin() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }
}

public UserService {
    //(...)

    public User getLoggedUser() {
        var login = LoggedUserInfo.getLogin();

        return getByLogin(login);
    }

    @Cacheable(value = "userByLogin", key = "#login")
    public User getByLogin(String login) {
        return userRepository.findByLogin(login);
    }
}
我的缓存配置为:

<cache alias="userByLogin">
        <key-type>java.lang.String</key-type>
        <value-type>com.myproject.User</value-type>
        <expiry>
            <ttl unit="hours">24</ttl>
        </expiry>
        <resources>
            <heap unit="MB">1</heap>
        </resources>
</cache>
或:

但如果我尝试这样做,则每次调用getLoggedUser并且从不使用缓存时都会出现警告:

WARN  o.e.i.i.store.heap.OnHeapStore - Max Object Graph Size reached for the object : [B@28c27a2f

我缺少什么?

您可以尝试增大最大对象图大小(默认值:1000)

您可以这样调整它:


java.lang.String
com.myproject.User
24
1.
10000
阅读后,我解决了将缓存配置更改为按条目数而不是内存使用限制的问题:

<cache alias="userByLogin">
        <key-type>java.lang.String</key-type>
        <value-type>com.myproject.User</value-type>
        <expiry>
            <ttl unit="hours">24</ttl>
        </expiry>
        <resources>
            <heap>150</heap>
        </resources>
</cache>

java.lang.String
com.myproject.User
24
150

我仍然无法理解为什么前一个版本使用UserService::getByLogin,但不使用UserService::getLoggedUser。

这解决了问题,但我更喜欢根据条目而不是内存使用情况更改堆限制(请参见我的答案)。完美!如果你能把任何一个答案标记为被接受,那就好了。祝您有个美好的一天。
WARN  o.e.i.i.store.heap.OnHeapStore - Max Object Graph Size reached for the object : [B@28c27a2f
<cache alias="userByLogin">
        <key-type>java.lang.String</key-type>
        <value-type>com.myproject.User</value-type>
        <expiry>
            <ttl unit="hours">24</ttl>
        </expiry>
        <resources>
            <heap>150</heap>
        </resources>
</cache>