Created
July 31, 2017 15:33
-
-
Save yusufunlu/3b7a7027e84d5e36d130dcc375aa8c19 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.unlu.co | |
import java.util.concurrent.ConcurrentHashMap; | |
import com.google.gson.Gson; | |
import org.json.JSONObject; | |
public class CacheManager { | |
private static CacheManagerServiceData mCacheManager; | |
private Gson gson = new Gson(); | |
public static ConcurrentHashMap<String, String> mActivityCache = new ConcurrentHashMap<String, String>(); | |
public static ConcurrentHashMap<String, JSONObject> mActivityJsonCache = new ConcurrentHashMap<String, JSONObject>(); | |
public static synchronized CacheManagerServiceData getInstance() { | |
if (mCacheManager == null) | |
mCacheManager = new CacheManagerServiceData(); | |
return mCacheManager; | |
} | |
public CacheManager() { | |
if (mActivityCache == null) { | |
mActivityCache = new ConcurrentHashMap<String, String>(); | |
} | |
if (mActivityJsonCache == null) { | |
mActivityJsonCache = new ConcurrentHashMap<String, JSONObject>(); | |
} | |
} | |
public <T> void setActivityCache(String name, T data) | |
{ | |
String _dataString; | |
if(JSONObject.class == data.getClass()) | |
{ | |
mActivityJsonCache.put(name, (JSONObject) data); | |
} | |
else | |
{ | |
_dataString = gson.toJson(data); | |
mActivityCache.put(name,_dataString); | |
} | |
} | |
public <T> T getActivityCache(String name, Class<T> clazz) | |
{ | |
String _string; | |
JSONObject _jsonObject; | |
T _returnObject = null; | |
try { | |
if(clazz == JSONObject.class) | |
{ | |
_jsonObject = mActivityJsonCache.get(name); | |
_returnObject = (T) _jsonObject; | |
} | |
else | |
{ | |
_string = mActivityCache.get(name); | |
_returnObject = gson.fromJson(_string,clazz); | |
} | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
return _returnObject; | |
} | |
public void removeActivityCache(String name) | |
{ | |
if(mActivityCache.contains(name)) | |
{ | |
mActivityCache.remove(name); | |
} | |
else | |
{ | |
mActivityJsonCache.remove(name); | |
} | |
} | |
public static void dropAllCacheData() { | |
if (mActivityCache != null) | |
{ | |
mActivityCache.clear(); | |
} | |
if (mActivityJsonCache != null) | |
{ | |
mActivityJsonCache.clear(); | |
} | |
} | |
} |
Selam , 2 sene sonra bu mesaji yeni gordum kusuruma bakmayin :) Ben bu cachi androidde kullaniyordum. Cache datasini de File Systeme yazmak icin Android'deki share preference apisini kullaniyorum. O da String istiyor datayi o yuzden yapmistim
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Selamlar. Cache lerken String e cevirmenizin nedeni nedir?