public MongoMetaCacheManager(int maxCacheSize, int cacheExpiredTime, int collectionCountCacheSize, int collectionCountCacheExpiredTime) { this.nameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime); this.pluralNameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime); this.countCache = new ExpirableCache<Integer>(collectionCountCacheSize, collectionCountCacheExpiredTime); }
public void putCountToCache(String dbCollectionName, Integer count){ countCache.putObject(dbCollectionName, count); } }
public void putObject(String name, T value) { checkNotNull(name); long now = System.currentTimeMillis(); cleanup(now); CacheEntry<T> entry = new CacheEntry<T>(now, value); cache.put(name, entry); }
@Test public void sizeTest() { ExpirableCache<String> cache = new ExpirableCache<String>(1, 60); cache.putObject(c[0], c[0]); cache.putObject(c[1], c[1]); assertNull(cache.getObject(c[0])); assertTrue(cache.getObject(c[1]) == c[1]); }
@Test public void deleteTest() { ExpirableCache<String> cache = new ExpirableCache<String>(100, 60); cache.putObject(c[0], c[0]); assertTrue(cache.getObject(c[0]) == c[0]); cache.deleteObject(c[0]); assertNull(cache.getObject(c[0])); }
@Override public List<Repository> getRepositories(MetadataContext ctx) { MetadataContext context = ctx != null ? ctx : new MetadataContext(); if (!context.isRefreshRepsitory()) { return cache.values(); } ExpirableCache<Repository> newCache = new ExpirableCache<Repository>(maxCacheSize, cacheExpiredTime); BasicDBObject query = new BasicDBObject(); query.put(Repository.STATE_FIELD, Repository.StateEnum.normal.toString()); List<Repository> result = new ArrayList<Repository>(); DBCursor cursor = repoCollection.find(query); while (cursor.hasNext()) { DBObject object = cursor.next(); Repository r = repositoryConverter.fromBson(object, Repository.class); createServiceForRepository(r); result.add(r); newCache.putObject(r.getRepositoryName(), r); } cache = newCache; return result; }
/** * * className is either metaclass name or pluralName * * @param className * @return */ public void deleteMetaClassFromCache(MetaClass m) { nameCache.deleteObject(m.getName()); String pluralName = m.getpluralName(); if (pluralName != null) { pluralNameCache.deleteObject(pluralName); } }
public List<MetaClass> getMetaClassesFromCache() { return this.nameCache.values(); }
public void deleteObject(String name) { checkNotNull(name); cache.remove(name); }
@Test public void cacheTest() { ExpirableCache<String> cache = new ExpirableCache<String>(100, 60); for (String i : c) { cache.putObject(i, i); } for (String i : c) { String o = cache.getObject(i); assertTrue(o == c[Integer.valueOf(o)]); } }
@Override public List<Repository> getRepositories(AccessType type) { List<Repository> allRepos = cache.values(); List<Repository> result = new ArrayList<Repository>(); for (Repository repo : allRepos) { if (repo.getAccessType() == type) { result.add(repo); } } return result; }
public T getObject(String name) { checkNotNull(name); CacheEntry<T> entry = cache.get(name); if (entry == null) { return null; } long now = System.currentTimeMillis(); if (now - entry.getLastModified() > timeToLive) { return null; } return entry.getValue(); }
@Test public void expirationTest() throws InterruptedException { ExpirableCache<String> cache = new ExpirableCache<String>(100, 1); cache.putObject(c[0], c[0]); assertTrue(cache.getObject(c[0]) == c[0]); Thread.sleep(2000); assertNull(cache.getObject(c[0])); }
@Override public final List<MetaClass> getMetaClasses(MetadataContext ctx) { MetadataContext context = ctx != null ? ctx : new MetadataContext(); ArrayList<MetaClass> result = new ArrayList<MetaClass>(); if (!context.isRefreshMetadata()) { return cacheManager.getMetaClassesFromCache(); } DBCursor cursor = collection.find(); while(cursor.hasNext()) { DBObject o = cursor.next(); MetaClass m = converter.fromBson(o, MetaClass.class); result.add(m); } MetaClassGraph newGraph = new MetaClassGraph(result); ExpirableCache<MetaClass> newNameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime); ExpirableCache<MetaClass> pluralNameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime); for (MetaClass m : result) { setUpMetaClass(m, newGraph); cacheManager.addMetaClassToCache(m, newNameCache, pluralNameCache); newGraph.updateMetaClass(m); } // ## this might still not thread safe between the cache and graphs. But the time window should be small enough this.graph = newGraph; cacheManager.refreshCache(newNameCache, pluralNameCache); return result; }
public Integer getCountFromCache(String dbCollectionName){ return countCache.getObject(dbCollectionName); }
@Override public void setHistoryService(IMetadataHistoryService historyService) { this.historyService = historyService; List<Repository> repositories = cache.values(); for(Repository repo : repositories) { IMetadataService metaService = repo.getMetadataService();
public MongoRepositoryServiceImpl(Mongo mongo, int maxCacheSize, int cacheExpiredTime, int collectionCountCacheSize, int collectionCountCacheExpiredTime, ICMSLock metadataLock, WriteConcern writeConcern) { CheckConditions.checkNotNull(mongo, "mongo can not be null"); CheckConditions.checkArgument(maxCacheSize > 0, "maximumSize can not be negtive value"); CheckConditions.checkArgument(cacheExpiredTime > 0, "expireAfterSeconds can not be negtive value"); CheckConditions.checkArgument(metadataLock != null, "metadataLock should not be null"); this.metadataLock = metadataLock; this.mongo = mongo; this.maxCacheSize = maxCacheSize; this.cacheExpiredTime = cacheExpiredTime; cache = new ExpirableCache<Repository>(maxCacheSize, cacheExpiredTime); this.collectionCountCacheSize = collectionCountCacheSize; this.collectionCountCacheExpiredTime = collectionCountCacheExpiredTime; prepareSystemDB(mongo); repoCollection = this.mongo.getDB(CMSConsts.SYS_DB).getCollection(CMSConsts.REPOSITORY_COLL); // read from primary only repoCollection.setReadPreference(ReadPreference.primary()); repoCollection.setWriteConcern(writeConcern); }