Skip to content

Commit

Permalink
added a method to set debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrutikaGirme committed Apr 14, 2023
1 parent 9cacdc7 commit e4cbe14
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions objectcache/src/main/java/com/myntra/objectcache/ObjectCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.myntra.objectcache.BuildConfig;

/**
* @author mario
*/
Expand All @@ -25,6 +23,8 @@ public class ObjectCache {
static final String STRING_KEY_PATTERN = "[a-z0-9_-]{1,120}";
public static final Pattern LEGAL_KEY_PATTERN = Pattern.compile(STRING_KEY_PATTERN);

private static boolean debugMode = false;

public static ObjectCache defaultCache(File cacheDir, int appVersion) {
return cacheInstance(cacheDir, DEFAULT_CACHE_DIR, appVersion);
}
Expand All @@ -35,7 +35,7 @@ public static ObjectCache cacheInstance(File cacheDir, String folder, int appVer
try {
objectCache.cache = DiskLruCache.open(file, appVersion, 1, SIZE);
} catch (IOException e) {
if(BuildConfig.DEBUG)
if(debugMode)
e.printStackTrace();
}
return objectCache;
Expand All @@ -47,12 +47,16 @@ public static ObjectCache cacheInstance(File cacheDir, String folder, int appVer
try {
objectCache.cache = DiskLruCache.open(file, appVersion, 1, size);
} catch (IOException e) {
if(BuildConfig.DEBUG)
if(debugMode)
e.printStackTrace();
}
return objectCache;
}

public static void setDebugBuild(boolean mode) {
debugMode = mode;
}

public void write(String key, Object object, long expiry) {
key = sanitizeKey(key);
try {
Expand All @@ -63,7 +67,7 @@ public void write(String key, Object object, long expiry) {
cache.flush();
editor.commit();
} catch (IOException e) {
if(BuildConfig.DEBUG)
if(debugMode)
e.printStackTrace();
}
}
Expand All @@ -85,7 +89,7 @@ public <T> T fetch(String key, Class<T> classOfT) {
}
}
} catch (Exception e) {
if(BuildConfig.DEBUG)
if(debugMode)
e.printStackTrace();
}
return null;
Expand All @@ -101,7 +105,7 @@ public <T> T fetchEvenIfExpired(String key, Class<T> classOfT) {
return gson.fromJson(entryJson.getAsJsonObject(Entry.VALUE), classOfT);
}
} catch (IOException e) {
if(BuildConfig.DEBUG)
if(debugMode)
e.printStackTrace();
}
return null;
Expand All @@ -117,7 +121,7 @@ public Boolean hasExpired(String key) {
return entry.hasExpired();
}
} catch (IOException e) {
if(BuildConfig.DEBUG)
if(debugMode)
e.printStackTrace();
}
return null;
Expand All @@ -128,7 +132,7 @@ public boolean remove(String key) {
try {
return cache.remove(key);
} catch (IOException e) {
if(BuildConfig.DEBUG)
if(debugMode)
e.printStackTrace();
}
return false;
Expand Down

0 comments on commit e4cbe14

Please sign in to comment.