7.10.2. OnUserCache 回调接口


您可能需要缓存特定于您的供应商实现的额外信息。每当用户缓存时,User Storage SPI 都有一个回调: org.keycloak.models.cache.OnUserCache

public interface OnUserCache {
    void onCache(RealmModel realm, CachedUserModel user, UserModel delegate);
}

如果您希望此回调,您的供应商类应该实现这个接口。UserModel delegate 参数是您的供应商返回的 UserModel 实例。CachedUserModel 是一个展开的 UserModel 接口。这是本地缓存在本地存储中的实例。

public interface CachedUserModel extends UserModel {

    /**
     * Invalidates the cache for this user and returns a delegate that represents the actual data provider
     *
     * @return
     */
    UserModel getDelegateForUpdate();

    boolean isMarkedForEviction();

    /**
     * Invalidate the cache for this model
     *
     */
    void invalidate();

    /**
     * When was the model was loaded from database.
     *
     * @return
     */
    long getCacheTimestamp();

    /**
     * Returns a map that contains custom things that are cached along with this model.  You can write to this map.
     *
     * @return
     */
    ConcurrentHashMap getCachedWith();
}

CachedUserModel 接口允许您从缓存中驱除用户并获取供应商 UserModel 实例。getCachedWith () 方法返回一个映射,允许您缓存与用户相关的其他信息。例如,凭证不是 UserModel 接口的一部分。如果要在内存中缓存凭证,则需要实施 OnUserCache,并使用 getCachedWith () 方法缓存用户的凭据。

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部