Search in sources :

Example 41 with CachingProvider

use of javax.cache.spi.CachingProvider in project gora by apache.

the class JCacheStore method initialize.

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) {
    super.initialize(keyClass, persistentClass, properties);
    CachingProvider cachingProvider = Caching.getCachingProvider(properties.getProperty(GORA_DEFAULT_JCACHE_PROVIDER_KEY));
    if (properties.getProperty(JCACHE_CACHE_NAMESPACE_PROPERTY_KEY) != null) {
        goraCacheNamespace = properties.getProperty(JCACHE_CACHE_NAMESPACE_PROPERTY_KEY);
    }
    try {
        this.persistentDataStore = DataStoreFactory.getDataStore(keyClass, persistentClass, new Configuration());
    } catch (GoraException ex) {
        LOG.error("Couldn't initialize persistent DataStore.", ex);
    }
    if (properties.getProperty(GORA_DEFAULT_JCACHE_PROVIDER_KEY).contains(HAZELCAST_SERVER_CACHE_PROVIDER_IDENTIFIER)) {
        Config config = new ClasspathXmlConfig(properties.getProperty(GORA_DEFAULT_JCACHE_HAZELCAST_CONFIG_KEY));
        hazelcastInstance = Hazelcast.newHazelcastInstance(config);
    } else {
        try {
            ClientConfig config = new XmlClientConfigBuilder(properties.getProperty(GORA_DEFAULT_JCACHE_HAZELCAST_CONFIG_KEY)).build();
            hazelcastInstance = HazelcastClient.newHazelcastClient(config);
        } catch (IOException ex) {
            LOG.error("Couldn't locate the client side cache provider configuration.", ex);
        }
    }
    Properties providerProperties = new Properties();
    providerProperties.setProperty(HazelcastCachingProvider.HAZELCAST_INSTANCE_NAME, hazelcastInstance.getName());
    try {
        manager = cachingProvider.getCacheManager(new URI(goraCacheNamespace), null, providerProperties);
    } catch (URISyntaxException ex) {
        LOG.error("Couldn't initialize cache manager to bounded hazelcast instance.", ex);
        manager = cachingProvider.getCacheManager();
    }
    if (((properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY) != null) && Boolean.valueOf(properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY))) || ((manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass) == null))) {
        cacheEntryList = new ConcurrentSkipListSet<>();
        cacheConfig = new CacheConfig<K, T>();
        cacheConfig.setTypes(keyClass, persistentClass);
        if (properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY) != null) {
            cacheConfig.setReadThrough(Boolean.valueOf(properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY)));
        } else {
            cacheConfig.setReadThrough(true);
        }
        if (properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY) != null) {
            cacheConfig.setWriteThrough(Boolean.valueOf(properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY)));
        } else {
            cacheConfig.setWriteThrough(true);
        }
        if (properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY) != null) {
            cacheConfig.setStoreByValue(Boolean.valueOf(properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY) != null) {
            cacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY) != null) {
            cacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_POLICY_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.valueOf(properties.getProperty(JCACHE_EVICTION_POLICY_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_MAX_SIZE_POLICY_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.valueOf(properties.getProperty(JCACHE_EVICTION_MAX_SIZE_POLICY_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_SIZE_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setSize(Integer.valueOf(properties.getProperty(JCACHE_EVICTION_SIZE_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY) != null) {
            String expiryPolicyIdentifier = properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY);
            if (expiryPolicyIdentifier.equals(JCACHE_ACCESSED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_CREATED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_MODIFIED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ModifiedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_TOUCHED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new TouchedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            }
        }
        if (properties.getProperty(HAZELCAST_CACHE_IN_MEMORY_FORMAT_PROPERTY_KEY) != null) {
            String inMemoryFormat = properties.getProperty(HAZELCAST_CACHE_IN_MEMORY_FORMAT_PROPERTY_KEY);
            if (inMemoryFormat.equals(HAZELCAST_CACHE_BINARY_IN_MEMORY_FORMAT_IDENTIFIER) || inMemoryFormat.equals(HAZELCAST_CACHE_OBJECT_IN_MEMORY_FORMAT_IDENTIFIER) || inMemoryFormat.equals(HAZELCAST_CACHE_NATIVE_IN_MEMORY_FORMAT_IDENTIFIER)) {
                cacheConfig.setInMemoryFormat(InMemoryFormat.valueOf(inMemoryFormat));
            }
        }
        cacheConfig.setCacheLoaderFactory(JCacheCacheFactoryBuilder.factoryOfCacheLoader(this.persistentDataStore, keyClass, persistentClass));
        cacheConfig.setCacheWriterFactory(JCacheCacheFactoryBuilder.factoryOfCacheWriter(this.persistentDataStore, keyClass, persistentClass));
        cache = manager.createCache(persistentClass.getSimpleName(), cacheConfig).unwrap(ICache.class);
    } else {
        cache = manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass).unwrap(ICache.class);
        this.populateLocalCacheEntrySet(cache);
    }
    cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(JCacheCacheFactoryBuilder.factoryOfEntryListener(new JCacheCacheEntryListener<K, T>(cacheEntryList)), null, true, true));
    LOG.info("JCache Gora datastore initialized successfully.");
}
Also used : CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) Configuration(org.apache.hadoop.conf.Configuration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheConfig(com.hazelcast.config.CacheConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) ClasspathXmlConfig(com.hazelcast.config.ClasspathXmlConfig) ClasspathXmlConfig(com.hazelcast.config.ClasspathXmlConfig) Duration(javax.cache.expiry.Duration) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Properties(java.util.Properties) URI(java.net.URI) GoraException(org.apache.gora.util.GoraException) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ICache(com.hazelcast.cache.ICache) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastCachingProvider(com.hazelcast.cache.HazelcastCachingProvider) CachingProvider(javax.cache.spi.CachingProvider)

Example 42 with CachingProvider

use of javax.cache.spi.CachingProvider in project caffeine by ben-manes.

the class OSGiTest method sanity.

@Test
@Ignore("Requires jsr107 cache-api v1.0.1 (see https://github.com/jsr107/jsr107spec/issues/326)")
public void sanity() {
    CachingProvider cachingProvider = Caching.getCachingProvider("com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider", getClass().getClassLoader());
    Cache<Integer, Integer> cache = cachingProvider.getCacheManager().getCache("test-cache", Integer.class, Integer.class);
    assertNull(cache.get(1));
}
Also used : CachingProvider(javax.cache.spi.CachingProvider) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 43 with CachingProvider

use of javax.cache.spi.CachingProvider in project metrics by dropwizard.

the class JCacheGaugeSetTest method setUp.

@Before
public void setUp() throws Exception {
    CachingProvider provider = Caching.getCachingProvider();
    cacheManager = provider.getCacheManager(getClass().getResource("ehcache.xml").toURI(), getClass().getClassLoader());
    myCache = cacheManager.getCache("myCache");
    myOtherCache = cacheManager.getCache("myOtherCache");
    registry = new MetricRegistry();
    registry.register("jcache.statistics", new JCacheGaugeSet());
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) JCacheGaugeSet(com.codahale.metrics.jcache.JCacheGaugeSet) CachingProvider(javax.cache.spi.CachingProvider) Before(org.junit.Before)

Example 44 with CachingProvider

use of javax.cache.spi.CachingProvider in project hazelcast by hazelcast.

the class CacheResourceTest method testCloseableCacheWriter.

@Test
public void testCloseableCacheWriter() throws InterruptedException {
    CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(factory.newHazelcastInstance());
    CacheManager cacheManager = provider.getCacheManager();
    CloseableCacheWriter writer = new CloseableCacheWriter();
    Factory<CloseableCacheWriter> writerFactory = FactoryBuilder.factoryOf(writer);
    CompleteConfiguration<Object, Object> configuration = new CacheConfig().setCacheWriterFactory(writerFactory).setWriteThrough(true);
    Cache<Object, Object> cache = cacheManager.createCache("test", configuration);
    // trigger partition assignment
    cache.get("key");
    factory.newHazelcastInstance();
    for (int i = 0; i < 1000; i++) {
        cache.put(i, i);
        LockSupport.parkNanos(1000);
    }
    assertFalse("CacheWriter should not be closed!", writer.closed);
}
Also used : CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 45 with CachingProvider

use of javax.cache.spi.CachingProvider in project hazelcast by hazelcast.

the class CacheSerializationTest method test_CacheReplicationOperation_serialization.

@Test
public void test_CacheReplicationOperation_serialization() throws Exception {
    TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(1);
    HazelcastInstance hazelcastInstance = factory.newHazelcastInstance();
    try {
        CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(hazelcastInstance);
        CacheManager manager = provider.getCacheManager();
        CompleteConfiguration configuration = new MutableConfiguration();
        Cache cache1 = manager.createCache("cache1", configuration);
        Cache cache2 = manager.createCache("cache2", configuration);
        Cache cache3 = manager.createCache("cache3", configuration);
        for (int i = 0; i < 1000; i++) {
            cache1.put("key" + i, i);
            cache2.put("key" + i, i);
            cache3.put("key" + i, i);
        }
        HazelcastInstanceProxy proxy = (HazelcastInstanceProxy) hazelcastInstance;
        Field original = HazelcastInstanceProxy.class.getDeclaredField("original");
        original.setAccessible(true);
        HazelcastInstanceImpl impl = (HazelcastInstanceImpl) original.get(proxy);
        NodeEngineImpl nodeEngine = impl.node.nodeEngine;
        CacheService cacheService = nodeEngine.getService(CacheService.SERVICE_NAME);
        int partitionCount = nodeEngine.getPartitionService().getPartitionCount();
        for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
            CachePartitionSegment segment = cacheService.getSegment(partitionId);
            CacheReplicationOperation operation = new CacheReplicationOperation(segment, 1);
            Data serialized = service.toData(operation);
            try {
                service.toObject(serialized);
            } catch (Exception e) {
                throw new Exception("Partition: " + partitionId, e);
            }
        }
    } finally {
        factory.shutdownAll();
    }
}
Also used : HazelcastInstanceImpl(com.hazelcast.instance.HazelcastInstanceImpl) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) CachePartitionSegment(com.hazelcast.cache.impl.CachePartitionSegment) CacheReplicationOperation(com.hazelcast.cache.impl.operation.CacheReplicationOperation) Data(com.hazelcast.nio.serialization.Data) CachePartitionEventData(com.hazelcast.cache.impl.CachePartitionEventData) HazelcastInstanceProxy(com.hazelcast.instance.HazelcastInstanceProxy) MutableConfiguration(javax.cache.configuration.MutableConfiguration) UnknownHostException(java.net.UnknownHostException) Field(java.lang.reflect.Field) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CompleteConfiguration(javax.cache.configuration.CompleteConfiguration) CacheManager(javax.cache.CacheManager) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) Cache(javax.cache.Cache) CacheService(com.hazelcast.cache.impl.CacheService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CachingProvider (javax.cache.spi.CachingProvider)66 CacheManager (javax.cache.CacheManager)48 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)42 Test (org.junit.Test)33 QuickTest (com.hazelcast.test.annotation.QuickTest)28 HazelcastInstance (com.hazelcast.core.HazelcastInstance)24 ParallelTest (com.hazelcast.test.annotation.ParallelTest)24 CacheConfig (com.hazelcast.config.CacheConfig)21 HazelcastClientCachingProvider (com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)18 ClientConfig (com.hazelcast.client.config.ClientConfig)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 MutableConfiguration (javax.cache.configuration.MutableConfiguration)12 ICache (com.hazelcast.cache.ICache)10 HazelcastClientProxy (com.hazelcast.client.impl.HazelcastClientProxy)10 Cache (javax.cache.Cache)10 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)9 Config (com.hazelcast.config.Config)8 Data (com.hazelcast.nio.serialization.Data)8 Before (org.junit.Before)8 HazelcastCacheManager (com.hazelcast.cache.HazelcastCacheManager)7