2017-01-21 9 views
-1

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)に作成されますが、これらのファイルは空です。

問題を見つけることができませんでした。ログにエラーはありません。これはキャッシングを初めて行ったときです。これで私を助けてください。

ありがとうございました。

答えて

0

最後に私はこの問題を解決しました。 すべてのBean構成と依存性注入が正しく行われました。

私が欠けていたのは、UserDaoInt.SoではなくUserDaoImplを使用していたことです。私はLoginAdapterからUserDaoImplにキャッシュを移動し、Beanを定義するためにUserDaoIntを使用しました。 Beanが何らかのインタフェースを実装している場合、デフォルトでSpringはこのインタフェースに基づいてプロキシを生成します。

Here is a good article about proxy creation in Spring

しかし、もし私が望むならUserDaoImplを使うことができますが、私はUserDaoIntの実装を削除しなければなりません。

0

私自身が最初にこれを試してはいけないという謝罪です。あなたの@Cacheableキーが間違っていると思います。

@Cacheable(value="findUser", key="#userId")とお試しください。

これで解決しない場合は、教えてください。

+0

ありがとうございました。キーを更新しましたが、それでも動作しません – RishiPandey

関連する問題