当前位置: 首页 > 面试题库 >

带有复杂键的Spring @Cacheable

钮瀚
2023-03-14
问题内容

我在Spring(3.1)中使用以下@Cacheable:

Spring:

<?xml   version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
    xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 
                            http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
                            http://www.springframework.org/schema/data/mongo
                            http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
                            http://www.springframework.org/schema/cache 
                            http://www.springframework.org/schema/cache/spring-cache.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
                            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<!-- Ehcache library setup -->
<bean id="ehcache"  class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:config-location="classpath:ehcache.xml" />

Maven:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.5.3</version>
    </dependency>

要缓存的方法:

@Cacheable(value="cahceName", key="concat(#param1).concat(‘-’).concat(#param2)")
    public String cachedMethod(String param1,String param2)

las,当我调试代码时,我看到即使param1和param2相同(即未使用cahce),也多次调用了该缓存方法。

有任何想法吗?


问题答案:

密钥显示不正确-

您可能是说- @Cacheable(value="cacheName", key="#param1.concat(‘-’).concat(#param2)")

此外,如果在没有调试信息的情况下完成编译,则param1,param2参数名称将对表达式求值器不可用。相反,您可以通过以下方式使用p0,p1等引用它们:

@Cacheable(value="cahceName", key="#p0.concat('-').concat(#p1)")

更新:

我在这里进行了一页测试,以演示其工作原理-https://gist.github.com/3315275



 类似资料:
  • 我在使用复合主键创建实体时遇到问题,该键也是外键。这是我的表和关系表原理图。当我想创建新闻实体时,我收到了带有null creatingnews的错误消息。新闻翻译有复合主键,外键引用到新闻表。 这是我的代码: 新闻聚合 新闻翻译 标签 新闻语言ID 在NewsFactory中,我希望使用NewsTranslation创建NewsAggregate,但有错误消息NullPointer。 新闻工厂

  • 问题内容: 我使用spring数据休息作为基础。但是当实体具有复合主键时,我不知道如何通过提供主键来获得实体。 河类: RiverPK课程: RiverDAO类: 然后,我可以通过调用get http:// localhost:8080 / river / 获取河流数据,还可以通过调用post http:// localhost:8080 / river / {river json} 为db创建新

  • 问题内容: 我有以下型号: 我必须找到属于user是成员的组的所有帖子。我已经用这种方法做到了: 但它会产生无效的SQL: 我想这样查询: 不使用原始SQL怎么办? 问题答案: 像这样的事情应该为您工作,尽管它需要混入一些原始SQL

  • 问题内容: 我有以下情况: 在我的Query.xml中,我以这种方式编写了插入内容: 阅读mybatis结果地图指南后,我尝试在mybatis-config.xml文件中添加以下行: 但我一直收到以下错误: 谁能告诉我该如何设置? 问题答案: 中的属性需要引用结果映射的名称,而不是Java类型: 但是,如果作为单独的表存储在数据库中,则不支持嵌套插入。您将需要在Java中调用两个插入。Result

  • 问题内容: 快速提问主要满足我对这个话题的好奇心。 我正在编写一些带有SQlite数据库后端的大型python程序,并且将来会处理大量记录,因此,我需要尽可能地优化。 对于一些功能,我正在字典中搜索关键字。我一直在使用“ in”关键字进行原型设计,并计划稍后返回并优化这些搜索,因为我知道“ in”关键字通常为O(n)(因为这仅表示python遍历整个列表并进行比较每个元素)。但是,由于python

  • 问题内容: 我有两个表,和。应该是不言自明的,并且包含所有变量的所有可能选择。 有一个外键,指出了它是哪个变量的选项。有一个外键,指出了当前选择的选项。 我用得到周围的循环关系上,并在“关系。但是,我想添加一个额外的数据库约束,以确保该约束是有效的(即,它所指的是一个引用它的选项)。 我需要的逻辑(假设它代表表中的一行)基本上是: 但是我不知道如何使用SQL,声明式或任何其他方法来构造这种约束。如