-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kaelaela/develop
Add cache
- Loading branch information
Showing
14 changed files
with
311 additions
and
104 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
59 changes: 59 additions & 0 deletions
59
library/src/main/java/me/kaelaela/opengraphview/OGCache.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package me.kaelaela.opengraphview; | ||
|
||
import android.graphics.Bitmap; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.util.LruCache; | ||
import android.text.TextUtils; | ||
import me.kaelaela.opengraphview.network.model.OGData; | ||
|
||
public class OGCache { | ||
|
||
private static OGCache mOGCache; | ||
private LruCache<String, OGData> mOGDataCache; | ||
private LruCache<String, Bitmap> mImageCache; | ||
|
||
public static OGCache getInstance() { | ||
if (mOGCache == null) { | ||
mOGCache = new OGCache(); | ||
} | ||
return mOGCache; | ||
} | ||
|
||
private OGCache() { | ||
final int max = (int) (Runtime.getRuntime().maxMemory() / 1024); | ||
final int cacheSize = max / 8; | ||
mOGDataCache = new LruCache<>(cacheSize); | ||
mImageCache = new LruCache<>(cacheSize); | ||
} | ||
|
||
public void add(String url, OGData ogData) { | ||
if (get(url) == null) { | ||
mOGDataCache.put(url, ogData); | ||
} | ||
} | ||
|
||
@Nullable | ||
public OGData get(String url) { | ||
if (TextUtils.isEmpty(url)) { | ||
return null; | ||
} | ||
return mOGDataCache.get(url); | ||
} | ||
|
||
public void addImage(String url, Bitmap bitmap) { | ||
if (TextUtils.isEmpty(url) || bitmap == null) { | ||
return; | ||
} | ||
if (getImage(url) == null) { | ||
mImageCache.put(url, bitmap); | ||
} | ||
} | ||
|
||
@Nullable | ||
public Bitmap getImage(String url) { | ||
if (TextUtils.isEmpty(url)) { | ||
return null; | ||
} | ||
return mImageCache.get(url); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.