【问题标题】:Spring Ehcache not workingSpring Ehcache 不工作
【发布时间】:2018-04-11 16:29:13
【问题描述】:

我已经使用 ehcache 在我的 Spring MVC 中实现了声明式缓存。 下面是 Spring 配置代码。

<cache:annotation-driven />

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
            <property name="cacheManager" ref="ehcache" />
    </bean>

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
        <property name="shared" value="true"/>
    </bean>

<bean id="UserDaoImpl" class="org.kmsg.dao.impl.UserDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

下面是ehcache xml配置。

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">
    <diskStore path="c:\\cache" />

    <cache name="findUser"
        maxEntriesLocalHeap="10000"
        maxEntriesLocalDisk="1000"
        eternal="false"
        diskSpoolBufferSizeMB="20"
        timeToIdleSeconds="300" timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU"
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

这是我要实现缓存的适配器类:

    public class LoginAdapter
    {
        static UserDaoImpl daoimpl =(UserDaoImpl)MainAdapter.context.getBean("UserDaoImpl");

        @Cacheable(value="findUser", key="#userId")
        public UserModel checkLogin1(String userId,String password)
        {
            UserModel   model = daoimpl.selectUserInfo(userId);         

            return model;
        }
}

用户道代码:

public class UserDaoImpl implements UserDaoInt
{
        JdbcTemplate jdbc=new JdbcTemplate();

        @Override
        public void setDataSource(DataSource dataSource)
        {
            jdbc=new JdbcTemplate(dataSource);
        }

        @Override
        public UserModel selectUserInfo(String userId) 
        {
            String sql  =   "SELECT "
                    + "user_id, "
                    + "password, "
                    + "no_of_device, "
                    + "email_id, "
                    + "otp, "
                    + "approved, "
                    + "secret_code, "
                    + "os, "
                    + "version, "
                    + "version_name, "
                    + "mobile_maker, "
                    + "mobile_model "
                    + "FROM user "
                    + "WHERE user_id=?; ";

            System.out.println("calling......");
            return jdbc.queryForObject(sql,new Object[]{userId},new UserMapper());
        }
}

最后这是服务:

@RequestMapping(value="/login" , method = RequestMethod.POST, headers="Accept=application/json")
    public UserModel checkLogin1(@RequestParam Map<String, String> params)
    {
        String userid   =   params.get("userId");
        String password =   params.get("password");

        return adapter.checkLogin1(userid, password);
    }

当我运行项目并调用服务时,每次从数据库而不是从缓存中调用数据。但是缓存文件是在指定位置(c:\cache)创建的,但是这些文件是空的。

我找不到问题。日志中没有错误。这是我第一次做缓存。请帮我解决这个问题。

谢谢。

【问题讨论】:

    标签: java spring-mvc ehcache


    【解决方案1】:

    我很抱歉没有先自己尝试这个。我的想法是您的@Cacheable 键不正确。

    请尝试@Cacheable(value="findUser", key="#userId")

    如果这不起作用,请告诉。

    【讨论】:

    • 感谢您的宝贵时间。我更新了密钥,但它仍然不起作用
    【解决方案2】:

    最后我解决了这个问题。 所有的 bean 配置和依赖注入都是正确的。

    我缺少的是我使用的是 UserDaoImpl 而不是 UserDaoInt。所以我将缓存从 LoginAdapter 移动到 UserDaoImpl 并使用 UserDaoInt 来定义 bean。因为如果 bean 实现了某个接口,那么 Spring 默认会基于这个接口创建代理。

    Here is a good article about proxy creation in Spring.

    但是,如果我愿意,我可以使用 UserDaoImpl,但是我必须删除 UserDaoInt 的实现。

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多