Hello All:
Volley is an excellent framework to make network calls from with in the android application. However sometimes we want to reduce cache size by clearing all the expired entries.
In this post I will share the code to achieve the same. I needed the list of all the cached keys. However current Volley code does not have any support for the same. I have taken the base code from here.
So I extended Cacheto add
Then updated DiskBasedCache.cs to implement this method
The compiled Volley.jar can be found from here.
Hope this helps. Happy coding :)
Volley is an excellent framework to make network calls from with in the android application. However sometimes we want to reduce cache size by clearing all the expired entries.
In this post I will share the code to achieve the same. I needed the list of all the cached keys. However current Volley code does not have any support for the same. I have taken the base code from here.
So I extended Cacheto add
public List<String> getKeysList();
Then updated DiskBasedCache.cs to implement this method
@Override public synchronized List<String> getKeysList() { if (mEntries != null && mEntries.size() > 0) { List<String> keys = new ArrayList<String>(); keys.addAll(mEntries.keySet()); return keys; } return null; }Following helper method helps in deleting stale cache entries
public static void clearStaleCacheEntries() { Cache cache = BaseApplication.getInstance().getRequestQueue().getCache(); if (cache != null) { if (cache.getKeysList() != null) { List<String> keys = cache.getKeysList(); for (String key : keys) { Cache.Entry entry = cache.get(key); if (entry != null) { if (entry.isExpired()) { cache.remove(key); } } } } } }Use following Async task to call this method
public class CacheClearAsyncTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { try { Helpers.clearStaleCacheEntries(); } catch (Exception exception) { exception.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { } }
The compiled Volley.jar can be found from here.
Hope this helps. Happy coding :)
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.
Keep visiting and sharing.
Thanks,
Ashwani.
0 comments :
Post a Comment