Question

Have a look at LruCacheObjectPersister.java, this is an in-memory object persister, based on the Android LRUCache. I want to extend this base class to cache any pojos class beside String or bitmap in memory. My goal is execute many kinds of requests in memory and get the result in cache, it look like this:

  class Request1 extends SpiceRequest<Book>{}
  class Request2 extends SpiceRequest<Category>{}

  getSpiceManager().execute(new Request1(), CACHE_KEY, 5 * DurationInMillis.ONE_MINUTE, new RequestListener<Book>(){}); 

  getSpiceManager().execute(new Request2(), CACHE_KEY, 5 * DurationInMillis.ONE_MINUTE, new RequestListener<Category>(){});

My problem is how to tell the cache manager should support these type in spice service like this:

  @Override
public CacheManager createCacheManager(Application application) {
    CacheManager cacheManager = new CacheManager();

    // I want to store any types instead of bitmap
    LruCacheBitmapObjectPersister lruCacheBitmapObjectPersister = new LruCacheBitmapObjectPersister(inFileBitmapObjectPersister, 1024*1024);
    cacheManager.addPersister(inFileBitmapObjectPersister);

    return cacheManager;
} 

How can I do this?

Était-ce utile?

La solution

You should create a custom LruCacheXXXXObjectPersister and register it in a custom service. Follow how things are done with bitmaps, and keep the same mechanism for your own POJOs.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top