Android Open Source - mobile2-android Volatile Cache Manager






From Project

Back to project page mobile2-android.

License

The source code is released under:

Apache License

If you think the Android project mobile2-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ecollege.android.util;
/*from ww  w  . ja va2s.  c  o  m*/
import java.lang.ref.SoftReference;
import java.util.concurrent.ConcurrentHashMap;

import roboguice.util.Ln;

public class VolatileCacheManager {
  
  private static final int INITIAL_CACHE_CAPACITY = 255;
  
  protected class CacheEntry {
    public Object value;
    public long cachedAt;
    public CacheEntry(Object value, long cachedAt) {
      super();
      this.value = value;
      this.cachedAt = cachedAt;
    }
    public Boolean valueIsOlderThanMillis(long expirationInMillis) {
      if (System.currentTimeMillis() - cachedAt <= expirationInMillis) {
        return false;
      }
      return true;
    }
  }

  final protected ConcurrentHashMap<Object, SoftReference<CacheEntry>> cacheMap = new ConcurrentHashMap<Object, SoftReference<CacheEntry>>(INITIAL_CACHE_CAPACITY);
  
  protected final long expirationInMillis;
  
  public VolatileCacheManager(long expirationInMillis) {
    super();
    this.expirationInMillis = expirationInMillis;
  }

  public void put(Object key, Object value) {
    // TODO: limit the size of the cache more manually than with SoftReference?
    // TODO: Add a TTL
    Ln.i(String.format("Cache put key: %s, value: %s", key, value.toString()));
    cacheMap.put(key, new SoftReference<CacheEntry>(new CacheEntry(value, System.currentTimeMillis())));
  }
  
  public <CachedT> CachedT get(Object key, Class<CachedT> clazz) {
    SoftReference<CacheEntry> ref = cacheMap.get(key);
    CacheEntry cacheEntry = null;
    Object cachedObject = null;
    if (null != ref) {
      cacheEntry = ref.get();
      if (cacheEntry != null) {
        if (cacheEntry.valueIsOlderThanMillis(expirationInMillis)) {
          cacheMap.remove(key);
          Ln.i(String.format("Cache key was expired: %s", key));
        } else {
          cachedObject = cacheEntry.value;
        }
      }
    }
    if (null == cachedObject) {
      Ln.i( String.format("Cache miss for key: %s", key));
      return null;
    } else {
      Ln.i( String.format("Cache hit for key: %s", key));
      if (null != clazz) {
        try {
          CachedT castObject = clazz.cast(cachedObject);
          return castObject;
        } catch (ClassCastException cce) {
          Ln.i( String.format("Cache failed to cast object to Class: ", clazz.toString()));
          return null;
        }
      } else {
        return null;
      }
    }
  }
   public void clear() {
     cacheMap.clear();
   }
}




Java Source Code List

com.ecollege.android.AnnouncementActivity.java
com.ecollege.android.CourseActivity.java
com.ecollege.android.CourseAnnouncementsActivity.java
com.ecollege.android.CourseDiscussionsActivity.java
com.ecollege.android.CourseGradebookActivity.java
com.ecollege.android.CoursePeopleActivity.java
com.ecollege.android.CourseThreadActivity.java
com.ecollege.android.CoursesActivity.java
com.ecollege.android.DiscussionsActivity.java
com.ecollege.android.DropboxMessageActivity.java
com.ecollege.android.ECollegeApplication.java
com.ecollege.android.GradeActivity.java
com.ecollege.android.HomeActivity.java
com.ecollege.android.HtmlContentActivity.java
com.ecollege.android.LoginActivity.java
com.ecollege.android.MainActivity.java
com.ecollege.android.PersonActivity.java
com.ecollege.android.ProfileActivity.java
com.ecollege.android.SingleSignonActivity.java
com.ecollege.android.SplashActivity.java
com.ecollege.android.UserDiscussionActivity.java
com.ecollege.android.UserResponseActivity.java
com.ecollege.android.UserTopicActivity.java
com.ecollege.android.activities.ECollegeActivityHelper.java
com.ecollege.android.activities.ECollegeActivity.java
com.ecollege.android.activities.ECollegeDefaultActivity.java
com.ecollege.android.activities.ECollegeListActivity.java
com.ecollege.android.activities.ECollegeTabActivity.java
com.ecollege.android.adapter.ActivityFeedAdapter.java
com.ecollege.android.adapter.GroupedAdapter.java
com.ecollege.android.adapter.LoadMoreAdapter.java
com.ecollege.android.adapter.ParentAdapterObserver.java
com.ecollege.android.adapter.ResponseAdapter.java
com.ecollege.android.adapter.TopicsAdapter.java
com.ecollege.android.adapter.UberAdapter.java
com.ecollege.android.adapter.UberItem.java
com.ecollege.android.adapter.UpcomingEventsAdapter.java
com.ecollege.android.adapter.WaitingForApiAdapter.java
com.ecollege.android.errors.ECollegeAlertException.java
com.ecollege.android.errors.ECollegeException.java
com.ecollege.android.errors.ECollegePromptException.java
com.ecollege.android.errors.ECollegePromptRetryException.java
com.ecollege.android.tasks.ECollegeAsyncTask.java
com.ecollege.android.tasks.ServiceCallTask.java
com.ecollege.android.tasks.TaskPostProcessor.java
com.ecollege.android.util.CacheConfiguration.java
com.ecollege.android.util.DateTimeUtil.java
com.ecollege.android.util.FileCacheManager.java
com.ecollege.android.util.VolatileCacheManager.java
com.ecollege.android.view.HeaderView.java
com.ecollege.android.view.helpers.ResponseCountViewHelper.java