}
@Override
public ApplicationModel getApplicationById(String id, RealmModel realm) {
if (!cache.isEnabled()) return getDelegate().getApplicationById(id, realm);
CachedApplication cached = cache.getApplication(id);
if (cached != null && !cached.getRealm().equals(realm.getId())) {
cached = null;
}
if (cached == null) {
ApplicationModel model = getDelegate().getApplicationById(id, realm);
if (model == null) return null;
if (appInvalidations.contains(id)) return model;
cached = new CachedApplication(cache, getDelegate(), realm, model);
cache.addCachedApplication(cached);
} else if (appInvalidations.contains(id)) {
return getDelegate().getApplicationById(id, realm);
} else if (managedApplications.containsKey(id)) {
return managedApplications.get(id);