ehcacheを使用してSpring MVCで宣言的キャッシングを実装しました。 以下はSpringの設定コードです。Spring Ehcacheが動作しない
<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 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>
これは私がキャッシュを実装するアダプタクラスです:
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);
}
私はプロジェクトを実行し、th eサービスでは、データはキャッシュからではなくデータベースから呼び出されます。ただし、キャッシュファイルは指定された場所(c:\ cache)に作成されますが、これらのファイルは空です。
問題を見つけることができませんでした。ログにエラーはありません。これはキャッシングを初めて行ったときです。これで私を助けてください。
ありがとうございました。
ありがとうございました。キーを更新しましたが、それでも動作しません – RishiPandey