Skip to content

Instantly share code, notes, and snippets.

@yemyatthu1990
Last active February 9, 2017 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemyatthu1990/133beeb595a4df54bd767257f72c4c55 to your computer and use it in GitHub Desktop.
Save yemyatthu1990/133beeb595a4df54bd767257f72c4c55 to your computer and use it in GitHub Desktop.
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