Last active
February 9, 2017 00:51
-
-
Save yemyatthu1990/133beeb595a4df54bd767257f72c4c55 to your computer and use it in GitHub Desktop.
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
public class TownshipCacheImpl implements TownshipCache { | |
private static final long EXPIRATION_TIME = 60 * 10 * 1000; | |
@Override | |
public boolean isExpired() { | |
Realm realm = Realm.getDefaultInstance(); | |
if (realm.where(TownshipEntity.class).count() != 0) { | |
Date currentTime = new Date(System.currentTimeMillis()); | |
SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH); | |
Date lastUpdated = null; | |
try { | |
lastUpdated = ISO8601DATEFORMAT.parse(realm.where(TownshipEntity.class).findFirst().getLastUpdated()); | |
boolean isExpired = currentTime.getTime() - lastUpdated.getTime() > EXPIRATION_TIME; | |
if(isExpired){ | |
realm.beginTransaction(); | |
realm.delete(TownshipEntity.class); | |
realm.commitTransaction(); | |
realm.close(); | |
} | |
return isExpired; | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
return false; | |
} | |
@Override | |
public boolean isCached() { | |
Realm realm = Realm.getDefaultInstance(); | |
return realm.where(TownshipEntity.class).findAll() != null && realm.where(TownshipEntity.class).findAll().size() > 0; | |
} | |
@Override | |
public Observable<List<TownshipEntity>> get() { | |
List<TownshipEntity> townshipEntities = Realm.getDefaultInstance().where(TownshipEntity.class).findAll(); | |
return Observable.just(townshipEntities); | |
} | |
@Override | |
public void put(List<TownshipEntity> townshipEntities) { | |
Realm realm = Realm.getDefaultInstance(); | |
realm.beginTransaction(); | |
realm.copyToRealmOrUpdate(townshipEntities); | |
realm.commitTransaction(); | |
realm.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment