Created
May 12, 2017 16:52
-
-
Save yusufunlu/05020dc63ba09b9988bba4b4fa058d08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yusuf.iris.utils; | |
import android.content.Context; | |
import java.util.List; | |
import java.util.concurrent.ConcurrentHashMap; | |
import com.yusuf.iris.helpers.Helper; | |
import com.yusuf.iris.zitem.Session; | |
import com.google.gson.Gson; | |
import static com.digiturk.iris.helpers.Helper.PREF_USERSESSION; | |
public class CacheManagerServiceData { | |
private static CacheManagerServiceData mCacheManager; | |
public static ConcurrentHashMap<String, String> mGenericCache; | |
public static ConcurrentHashMap<String, Session> mSessionCache; | |
public static synchronized CacheManagerServiceData getInstance() { | |
if (mCacheManager == null) | |
mCacheManager = new CacheManagerServiceData(); | |
return mCacheManager; | |
} | |
public CacheManagerServiceData() { | |
if (mGenericCache == null) { | |
mGenericCache = new ConcurrentHashMap<String, String>(); | |
} | |
if (mSessionCache == null) { | |
mSessionCache = new ConcurrentHashMap<String, Session>(); | |
} | |
} | |
public void setSessionCache(Context _context, Session session) | |
{ | |
String _sessionString = new Gson().toJson(session); | |
mSessionCache.put(PREF_USERSESSION,session); | |
Helper.putPrefString(_context,PREF_USERSESSION,_sessionString); | |
} | |
public Session getSessionCache(Context _context) | |
{ | |
Session _session = mSessionCache.get(PREF_USERSESSION); | |
if(_session == null) | |
{ | |
_session = new Gson().fromJson(Helper.getPrefString(_context,PREF_USERSESSION),Session.class); | |
} | |
return _session; | |
} | |
public <T> void setGenericCache(Context _context, String name,T data) | |
{ | |
String _dataString = new Gson().toJson(data); | |
mGenericCache.put(name,_dataString); | |
Helper.putPrefString(_context,name,_dataString); | |
} | |
public <T> T getGenericCache(Context _context, String name,Class<T> clazz) | |
{ | |
T _session = new Gson().fromJson(mGenericCache.get(name),clazz); | |
if(_session == null) | |
{ | |
_session = new Gson().fromJson(Helper.getPrefString(_context,name),clazz); | |
} | |
return _session; | |
} | |
public <T> void addListDataToCache(List<T> cachedList, List<T> newList) { | |
if (cachedList == null) | |
cachedList = newList; | |
else | |
cachedList.addAll(newList); | |
} | |
public static void dropAllCacheData() { | |
if (mGenericCache != null) | |
{ | |
mGenericCache.clear(); | |
} | |
if (mSessionCache != null) | |
{ | |
mSessionCache.clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment