在缓存中存储值 - spring

时间:2016-09-27 18:38:06

标签: java spring caching ehcache

我正在申请春季申请。我必须从数据库中获取String值并存储在缓存中,以便整天使用相同的对象而不需要访问数据库。下面是示例代码。

ehcache.xml中

<cache name="attPriorityCache" maxElementsInMemory="6000"
    eternal="true" overflowToDisk="false" />

java class:

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
//imports here..
    class StoreData{

      @Inject
        private MyDAO myDAO;
        private static CacheManager singletonManager = null;

        private static CacheManager getInstance()
        {
            if (singletonManager == null)
            {
                singletonManager = CacheManager.create();
            }
            return singletonManager;
        }

       private Cache getCache()
        {
             //create a cache object
        }

      @PostConstruct
        public void loadData()
        {

    //get the data from database and store in cache
      final String attPriority = myDAO.getAttenderPriority(); //hits the database and get the value
     //store in cache

     if(!Strings.isNullOrEmpty(attPriority)){
                 //get the cache and remove using removeAll(), so that if value is changed in database it reflects

            }
        }

由于我是这个概念的新手,请建议我如何为上面的代码创建缓存对象,以及如何在缓存中保留字符串值(attPriority),以便它在缓存中保留一整天。 在getCache()方法中,如何创建缓存对象并在loadData()方法中使用它来保存作为字符串的attPriority值。任何建议都会有所帮助。

0 个答案:

没有答案