小编Alw*_*ing的帖子

RedisCacheManager不更新keyspace_misses

我正在使用用于缓存的spring-boot spring-data-redis 1.8.9.RELEASE RedisCacheManager实现CacheManager.我想要查看的一个指标是缓存命中/未命中率.为了实现这一点,我正在提取通过redis服务器公开的keyspace_hits和keyspace_misses,也可以通过redis_cli查看INFO STATS.问题是RedisCacheManager从不注册缓存未命中,即即使存在缓存"未命中",keyspace_misses也不会增加.

调试代码,我看到spring-data-redis实际上EXISTS在检索之前检查redis中的密钥.我看到了这种方法的意义,但是当EXISTS对redis服务器执行时,它没有注册缓存未命中.

有没有办法使用RedisCacheManager并注册缓存未命中?我知道我可以使用其他redis对象来实现这一目标,但我想知道是否可以使用标准的CacheManager实现来完成它?

编辑

理想的解决方案不会增加大量开销,我无法编辑redis服务器的配置.

RedisCacheManager从缓存中检索元素时使用的代码.通知Boolean exists:

public RedisCacheElement get(final RedisCacheKey cacheKey) {
    Assert.notNull(cacheKey, "CacheKey must not be null!");
    Boolean exists = (Boolean)this.redisOperations.execute(new RedisCallback<Boolean>() {
        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            return connection.exists(cacheKey.getKeyBytes());
        }
    });
    return !exists ? null : new RedisCacheElement(cacheKey, this.fromStoreValue(this.lookup(cacheKey)));
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将通过MONITOR高速缓存未命中在redis 上执行这些命令.再次注意,EXISTS根据代码执行:

为缓存未命中执行Redis命令

执行上述命令后,keyspace_misses即使存在高速缓存未命中,也不会递增:

在此输入图像描述

java spring redis spring-data-redis spring-boot

16
推荐指数
1
解决办法
508
查看次数

Spring Data MongoDB Lookup with Pipeline Aggregation

如何将以下 MongoDB 查询转换为 Java Spring 应用程序要使用的查询?我找不到使用pipeline提供的查找方法的方法。

这是我试图转换的查询。我还想指出,我没有使用,$unwind因为我希望将deliveryZipCodeTimings保留为返回对象中的分组集合。

db.getCollection('fulfillmentChannel').aggregate([
    {
        $match: {
            "dayOfWeek": "SOME_VARIABLE_STRING_1"
        }
    },
    {
        $lookup: {
            from: "deliveryZipCodeTiming",
            let: { location_id: "$fulfillmentLocationId" },
            pipeline: [{
                $match: {
                    $expr: {
                        $and: [
                            {$eq: ["$fulfillmentLocationId", "$$location_id"]},
                            {$eq: ["$zipCode", "SOME_VARIABLE_STRING_2"]}
                        ]
                    }
                }
            },
            { 
                $project: { _id: 0, zipCode: 1, cutoffTime: 1 } 
            }],
            as: "deliveryZipCodeTimings"
        }
    },
    {
        $match: {
            "deliveryZipCodeTimings": {$ne: []}
        }
    }
])
Run Code Online (Sandbox Code Playgroud)

spring mongodb spring-data aggregation-framework spring-data-mongodb

16
推荐指数
2
解决办法
2万
查看次数

Spring Boot 验证通过 ObjectMapper GET @RequestParam 映射的 JSON

验证在 Spring Boot 中传递到 GET REST 控制器的复杂 JSON 对象的最简单方法是什么,我正在使用com.fasterxml.jackson.databind.ObjectMapper 进行映射?

这是控制器:

@RestController
@RequestMapping("/products")
public class ProductsController {

@GetMapping
public ProductResponse getProducts(
        @RequestParam(value = "params") String requestItem
) throws IOException {
    final ProductRequest productRequest =
            new ObjectMapper()
                    .readValue(requestItem, ProductRequest.class);

    return productRetriever.getProductEarliestAvailabilities(productRequest);
}}
Run Code Online (Sandbox Code Playgroud)

我要验证的 DTO 请求对象:

public class ProductRequest {
private String productId;

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}}
Run Code Online (Sandbox Code Playgroud)

我正在考虑在请求 DTO 上使用注释,但是当我这样做时,它们不会触发任何类型的异常,即@NotNull。我试着使用不同组合@Validated在控制器以及@Valid@RequestParam并没有什么原因造成的验证来触发。

java rest spring fasterxml spring-boot

3
推荐指数
1
解决办法
5609
查看次数