You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

spring-data-commons 2中org.springframework.data.util.CacheValue替代方案咨询

替代org.springframework.data.util.CacheValue的方案(Spring Data Commons 2.x+)

Hey there, I ran into this exact issue when upgrading to Spring Data Commons 2.x a while back, so I can share what I found and the workarounds that worked for me.

First, a quick context: The org.springframework.data.util.CacheValue class was an internal utility used by Spring Data to wrap cached values (primarily to distinguish between a cached null value and a cache miss). It got removed in 2.x as part of a broader cleanup of internal APIs that weren't meant for public use.

Here are the best alternatives depending on what you were using CacheValue for:

1. 自己实现轻量包装类(替代原有核心功能)

If you were using CacheValue to explicitly track whether a value exists in the cache (instead of relying on null to mean "not present"), you can easily roll your own equivalent. It's a tiny class that replicates the original behavior:

public class CacheValue<T> {
    private final T value;
    private final boolean isPresent;

    private CacheValue(T value, boolean isPresent) {
        this.value = value;
        this.isPresent = isPresent;
    }

    // Create a CacheValue for an existing value (even if it's null)
    public static <T> CacheValue<T> of(T value) {
        return new CacheValue<>(value, true);
    }

    // Create a CacheValue representing a cache miss
    public static <T> CacheValue<T> absent() {
        return new CacheValue<>(null, false);
    }

    public T getValue() {
        return value;
    }

    public boolean isPresent() {
        return isPresent;
    }
}

This gives you the same control over distinguishing between cached nulls and misses that the original CacheValue provided.

2. 使用Spring Cache的标准类

If your code was integrating with Spring's caching abstraction (which is what Spring Data uses under the hood now), switch to org.springframework.cache.Cache.ValueWrapper. This is the standard way to wrap cached values in Spring:

  • When you retrieve a value from a Cache, it returns a ValueWrapper if the key exists (even if the value is null).
  • If the key isn't present in the cache, it returns null.

You can use it like this:

Cache cache = cacheManager.getCache("myCache");
Cache.ValueWrapper wrapper = cache.get("myKey");

if (wrapper != null) {
    // Value exists in cache (could be null)
    Object value = wrapper.get();
} else {
    // Cache miss
}

For most use cases, this is the cleanest approach since it aligns with Spring's official caching API.

3. 适配Spring Data 2.x的缓存策略

If you were using CacheValue in custom Repository extensions or query caching logic, note that Spring Data 2.x moved to using Spring Cache's annotations (@Cacheable, @CacheEvict, etc.) directly for Repository-level caching. You might not need a wrapper class at all anymore—just leverage the built-in caching abstractions.

For example, instead of manually handling CacheValue in your query code, you can annotate your Repository method:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    @Cacheable("users")
    Optional<User> findByEmail(String email);
}

Spring handles the cache checks, value wrapping, and misses automatically here.

内容的提问来源于stack exchange,提问作者K''

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:10:36