@Bean @Qualifier("RateLimit") public ReadWriteMap<String, GridBucketState> map() { DefaultCacheManager cacheManager = new DefaultCacheManager(); cacheManager.defineConfiguration("rateLimit", new ConfigurationBuilder().build()); AdvancedCache<String, GridBucketState> cache = cacheManager.<String, GridBucketState>getCache("rateLimit").getAdvancedCache(); FunctionalMapImpl<String, GridBucketState> functionalMap = FunctionalMapImpl.create(cache); return ReadWriteMapImpl.create(functionalMap); }
private void createDomainCache() { Configuration conf = makeRequiredConfiguration(); logger.debug("Configuration for {} is: {}", DOMAIN_CACHE_NAME, conf.toString()); this.cacheManager.defineConfiguration(DOMAIN_CACHE_NAME, conf); final DefaultCacheManager finalCacheManager = this.cacheManager; this.domainCache = doWithinBackingTransactionIfNeeded(new Callable<Cache<String, DataVersionHolder>>() { @Override public Cache<String, DataVersionHolder> call() { return finalCacheManager.getCache(DOMAIN_CACHE_NAME); } }); }
private void createSystemCache() { Configuration conf = makeRequiredConfiguration(); logger.debug("Configuration for {} is: {}", SYSTEM_CACHE_NAME, conf.toString()); this.cacheManager.defineConfiguration(SYSTEM_CACHE_NAME, conf); final DefaultCacheManager finalCacheManager = this.cacheManager; this.systemCache = doWithinBackingTransactionIfNeeded(new Callable<Cache<String, Object>>() { @Override public Cache<String, Object> call() { return finalCacheManager.getCache(SYSTEM_CACHE_NAME); } }); }
DefaultCacheManager manager = new DefaultCacheManager(); Configuration c = new ConfigurationBuilder().persistence() .addSingleFileStore() .location("/tmp/myDataStore") .maxEntries(5000); manager.defineConfiguration("myCache", c); Cache<Object, Object> cache = manager.getCache("myCache");
public static void main(String[] args) { // Construct a simple local cache manager with default configuration DefaultCacheManager cacheManager = new DefaultCacheManager(); // Define local cache configuration cacheManager.defineConfiguration("local", new ConfigurationBuilder().build()); // Obtain the local cache Cache<String, String> cache = cacheManager.getCache("local"); // Store some values int range = 10; IntStream.range(0, range).boxed().forEach(i -> cache.put(i + "-key", i + "-value")); // Map and reduce the keys int result = cache.keySet().stream() .map(e -> Integer.valueOf(e.substring(0, e.indexOf("-")))) .collect(() -> Collectors.summingInt(i -> i.intValue())); System.out.printf("Result = %d\n", result); // Stop the cache manager and release all resources cacheManager.stop(); }
private void createCache() { Configuration conf = makeRequiredConfiguration(); logger.debug("Configuration for {} is: {}", CACHE_NAME, conf.toString()); this.cacheManager.defineConfiguration(CACHE_NAME, conf); this.cache = this.cacheManager.getCache(CACHE_NAME).getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES); // final DefaultCacheManager finalCacheManager = this.cacheManager; // this.cache = doWithinBackingTransactionIfNeeded(new Callable<Cache<Object, Object>>() { // @Override // public Cache<Object, Object> call() { // return finalCacheManager.getCache(CACHE_NAME).getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES); // } // }); }
public static void main(String[] args) { // Construct a simple local cache manager with default configuration DefaultCacheManager cacheManager = new DefaultCacheManager(); // Define local cache configuration cacheManager.defineConfiguration("local", new ConfigurationBuilder().build()); // Obtain the local cache Cache<String, String> cache = cacheManager.getCache("local"); // Store a value cache.put("key", "value"); // Retrieve the value and print it out System.out.printf("key = %s\n", cache.get("key")); // Stop the cache manager and release all resources cacheManager.stop(); }
cacheManager.defineConfiguration("default", cacheConfiguration); Cache<String, String> cache = cacheManager.getCache("default");
public static void main(String[] args) throws Exception { DefaultCacheManager cacheManager = new DefaultCacheManager(); cacheManager.defineConfiguration("local", new ConfigurationBuilder().build()); AdvancedCache<String, String> cache = cacheManager.<String, String>getCache("local").getAdvancedCache(); FunctionalMapImpl<String, String> functionalMap = FunctionalMapImpl.create(cache);
private void initTransactionManager() { Configuration conf = makeRequiredConfiguration(); // for the dummy cache disable, cache loaders if any was configured ConfigurationBuilder confBuilder = new ConfigurationBuilder().read(conf); confBuilder.loaders().clearCacheLoaders(); conf = confBuilder.build(); String DUMMY_CACHE = "dummy-cache"; logger.debug("Configuration for {} is: {}", DUMMY_CACHE, conf.toString()); try { this.cacheManager.defineConfiguration(DUMMY_CACHE, conf); Cache<?, ?> dummyCache = this.cacheManager.getCache(DUMMY_CACHE); TransactionManager tm = dummyCache.getAdvancedCache().getTransactionManager(); this.transactionManager = tm; } catch (Exception e) { logger.error("Failed to get Repository TransactionManager", e); throw new PersistenceException(e); } }
private void initTransactionManager() { Configuration conf = makeRequiredConfiguration(); // for the dummy cache disable, cache loaders if any was configured ConfigurationBuilder confBuilder = new ConfigurationBuilder().read(conf); confBuilder.loaders().clearCacheLoaders(); conf = confBuilder.build(); String DUMMY_CACHE = "dummy-cache"; logger.debug("Configuration for {} is: {}", DUMMY_CACHE, conf.toString()); try { this.cacheManager.defineConfiguration(DUMMY_CACHE, conf); Cache<?, ?> dummyCache = this.cacheManager.getCache(DUMMY_CACHE); TransactionManager tm = dummyCache.getAdvancedCache().getTransactionManager(); this.transactionManager = tm; } catch (Exception e) { logger.error("Failed to get Repository TransactionManager", e); throw new PersistenceException(e); } }
for (Entry<String, ConfigurationBuilder> entry : holder.getNamedConfigurationBuilders().entrySet()) currentCacheManager.defineConfiguration(entry.getKey(), entry.getValue().build());
public static void main(String[] args) { // Construct a simple local cache manager with default configuration DefaultCacheManager cacheManager = new DefaultCacheManager(); // Define local cache configuration cacheManager.defineConfiguration("local", new ConfigurationBuilder().build()); // Obtain the local cache Cache<String, String> cache = cacheManager.getCache("local"); // Register a listener cache.addListener(new MyListener()); // Store some values cache.put("key1", "value1"); cache.put("key2", "value2"); cache.put("key1", "newValue"); // Stop the cache manager and release all resources cacheManager.stop(); }
public static RestServer createRest(RestServerConfigurationBuilder configuration, GlobalConfigurationBuilder globalConfigurationBuilder, ConfigurationBuilder cacheConfigurationBuilder, String... definedCaches) { RestServer nettyRestServer = new RestServer(); Configuration cacheConfiguration = cacheConfigurationBuilder.build(); DefaultCacheManager cacheManager = new DefaultCacheManager(globalConfigurationBuilder.build(), cacheConfiguration); for (String cache : definedCaches) { cacheManager.defineConfiguration(cache, cacheConfiguration); } nettyRestServer.start(configuration.build(), cacheManager); return nettyRestServer; } }
private EmbeddedCacheManager createCacheManagerProgrammatically() { System.out.println("Starting a cache manager with a programmatic configuration"); DefaultCacheManager cacheManager = new DefaultCacheManager( GlobalConfigurationBuilder.defaultClusteredBuilder() .transport().nodeName(nodeName).addProperty("configurationFile", "jgroups.xml") .build(), new ConfigurationBuilder() .clustering() .cacheMode(CacheMode.REPL_SYNC) .build() ); // The only way to get the "repl" cache to be exactly the same as the default cache is to not define it at all cacheManager.defineConfiguration("dist", new ConfigurationBuilder() .clustering() .cacheMode(CacheMode.DIST_SYNC) .hash().numOwners(2) .build() ); return cacheManager; }
public MetadataManager(DefaultCacheManager cacheManager){ Configuration cacheConfiguration = cacheManager.getCacheConfiguration(Support.AVRO_METADATA_CACHE_NAME); if (cacheConfiguration == null) { ConfigurationBuilder cfg = new ConfigurationBuilder(); CacheMode cacheMode = cacheManager.getDefaultCacheConfiguration().clustering().cacheMode().equals(CacheMode.LOCAL) ? CacheMode.LOCAL : CacheMode.REPL_SYNC; cfg .transaction().lockingMode(LockingMode.PESSIMISTIC).syncCommitPhase(true).syncRollbackPhase(true) .persistence().addSingleFileStore().location(System.getProperty("java.io.tmpdir") + "/" + cacheManager.getNodeAddress()) // mandatory .locking().isolationLevel(IsolationLevel.READ_COMMITTED).useLockStriping(false) .clustering().cacheMode(cacheMode) .stateTransfer().fetchInMemoryState(true) .dataContainer().keyEquivalence(new ByteArrayEquivalence()); // for HotRod compatibility if (cacheMode.equals(CacheMode.REPL_SYNC)) cfg.clustering().stateTransfer().awaitInitialTransfer(true); cacheManager.defineConfiguration(Support.AVRO_METADATA_CACHE_NAME, cfg.build()); this.cacheManager = cacheManager; this.marshaller= Externalizer.getInstance(); this.knownSchemas = new ConcurrentHashMap<>(); knownSchemas.put(Request.getClassSchema().getFullName(), Request.getClassSchema()); knownSchemas.put(Response.getClassSchema().getFullName(), Response.getClassSchema()); } }
public static RestServerHelper defaultRestServer(ConfigurationBuilder configuration, String... cachesDefined) { GlobalConfigurationBuilder globalConfigurationBuilder = new GlobalConfigurationBuilder(); globalConfigurationBuilder.addModule(PrivateGlobalConfigurationBuilder.class).serverMode(true); GlobalConfigurationBuilder globalConfiguration = globalConfigurationBuilder.nonClusteredDefault(); DefaultCacheManager cacheManager = new DefaultCacheManager(globalConfiguration.build(), configuration.build()); cacheManager.getClassWhiteList().addClasses(TestClass.class); for (String cacheConfiguration : cachesDefined) { cacheManager.defineConfiguration(cacheConfiguration, configuration.build()); } return new RestServerHelper(cacheManager); }
public static HotRodServer startHotRodServerWithoutTransport(HotRodServerConfigurationBuilder builder, String... definedCaches) { GlobalConfigurationBuilder globalConfiguration = new GlobalConfigurationBuilder(); Configuration cacheConfiguration = new ConfigurationBuilder() .encoding().key().mediaType(APPLICATION_OBJECT_TYPE) .encoding().value().mediaType(APPLICATION_OBJECT_TYPE) .build(); builder.startTransport(false); DefaultCacheManager cacheManager = new DefaultCacheManager(globalConfiguration.build(), cacheConfiguration); for (String cache : definedCaches) { cacheManager.defineConfiguration(cache, cacheConfiguration); } return startHotRodServer(cacheManager, builder); }