-2

I have implemented the declarative caching in my Spring MVC using ehcache. Below is code of Spring config.

<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>

Below is ehcache xml config.

<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>

This is adapter class where i want to implement caching:

    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;
        }
}

The User Dao code:

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());
        }
}

And finally this is the service:

@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);
    }

When i run the project and call the service, everytime the data is called from database and not from cache. However the cache files are created at the specified place(c:\cache) but these files are empty.

I could not find the problem. There is no error in logs. This is my first time of doing caching. Please help me on this.

Thanks.

2 Answers 2

0

My apologies for not trying this out on my own first. My thinking is that your @Cacheable key is incorrect.

Please try with @Cacheable(value="findUser", key="#userId").

If this doesn't work, do tell.

1
  • Thanks for your time. I updated the key but it still does not work Jan 23, 2017 at 6:04
0

Finally i solved the problem. All the bean configurations and dependency injections were right.

The thing i was missing is that i was using UserDaoImpl rather than UserDaoInt.So i moved the cacheable from LoginAdapter to UserDaoImpl and Used UserDaoInt for defining bean. Because if bean implements some interface then Spring by default will create proxy based on this interface.

Here is a good article about proxy creation in Spring.

However i can use UserDaoImpl if i want but then i have to remove the implementation of UserDaoInt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.