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); }
@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; }
@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; }
@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)]); } }
@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])); }
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); }
@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])); }
@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]); }