Search in sources :

Example 1 with CreatedExpiryPolicy

use of javax.cache.expiry.CreatedExpiryPolicy 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 2 with CreatedExpiryPolicy

use of javax.cache.expiry.CreatedExpiryPolicy in project ignite by apache.

the class CacheConfig method wordCache.

/**
     * Configure streaming cache.
     */
public static CacheConfiguration<AffinityUuid, String> wordCache() {
    CacheConfiguration<AffinityUuid, String> cfg = new CacheConfiguration<>("words");
    // Index all words streamed into cache.
    cfg.setIndexedTypes(AffinityUuid.class, String.class);
    // Sliding window of 1 seconds.
    cfg.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(SECONDS, 1))));
    return cfg;
}
Also used : AffinityUuid(org.apache.ignite.cache.affinity.AffinityUuid) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 3 with CreatedExpiryPolicy

use of javax.cache.expiry.CreatedExpiryPolicy in project ignite by apache.

the class GridCacheAbstractLocalStoreSelfTest method testEvict.

/**
     * @throws Exception If failed.
     */
public void testEvict() throws Exception {
    Ignite ignite1 = startGrid(1);
    IgniteCache<Object, Object> cache = ignite1.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 100L)));
    // Putting entry.
    for (int i = 0; i < KEYS; i++) cache.put(i, i);
    // Wait when entry
    U.sleep(200);
    // Check that entry is evicted from cache, but local store does contain it.
    for (int i = 0; i < KEYS; i++) {
        cache.localEvict(Arrays.asList(i));
        assertNull(cache.localPeek(i));
        assertEquals(i, (int) LOCAL_STORE_1.load(i).get1());
        assertEquals(i, cache.get(i));
    }
}
Also used : Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy)

Example 4 with CreatedExpiryPolicy

use of javax.cache.expiry.CreatedExpiryPolicy in project ignite by apache.

the class IgniteCacheClientNearCacheExpiryTest method testExpirationOnClient.

/**
     * @throws Exception If failed.
     */
public void testExpirationOnClient() throws Exception {
    Ignite ignite = grid(NODES - 1);
    assertTrue(ignite.configuration().isClientMode());
    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
    assertTrue(((IgniteCacheProxy) cache).context().isNear());
    for (int i = 0; i < KEYS_COUNT; i++) cache.put(i, i);
    CreatedExpiryPolicy plc = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 500));
    IgniteCache<Object, Object> cacheWithExpiry = cache.withExpiryPolicy(plc);
    for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) {
        cacheWithExpiry.put(i, i);
        assertEquals(i, cacheWithExpiry.localPeek(i));
    }
    U.sleep(1000);
    // Check size of near entries via reflection because entries is filtered for size() API call.
    IgniteEx igniteEx = (IgniteEx) ignite;
    GridCacheConcurrentMap map = GridTestUtils.getFieldValue(((GridCacheProxyImpl) igniteEx.cachex(DEFAULT_CACHE_NAME)).delegate(), GridCacheAdapter.class, "map");
    assertEquals(KEYS_COUNT, map.publicSize());
    assertEquals(KEYS_COUNT, cache.size());
    for (int i = 0; i < KEYS_COUNT; i++) assertEquals(i, cacheWithExpiry.localPeek(i));
    for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) assertNull(cache.localPeek(i));
}
Also used : GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy)

Example 5 with CreatedExpiryPolicy

use of javax.cache.expiry.CreatedExpiryPolicy in project ignite by apache.

the class GridCacheTtlManagerNotificationTest method testThatNotificationWorkAsExpected.

/**
     * @throws Exception If failed.
     */
public void testThatNotificationWorkAsExpected() throws Exception {
    try (final Ignite g = startGrid(0)) {
        final BlockingArrayQueue<Event> queue = new BlockingArrayQueue<>();
        g.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                queue.add(evt);
                return true;
            }
        }, EventType.EVT_CACHE_OBJECT_EXPIRED);
        final String key = "key";
        IgniteCache<Object, Object> cache = g.cache(DEFAULT_CACHE_NAME);
        ExpiryPolicy plc1 = new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100_000));
        cache.withExpiryPolicy(plc1).put(key + 1, 1);
        // Cleaner should see entry.
        Thread.sleep(1_000);
        ExpiryPolicy plc2 = new CreatedExpiryPolicy(new Duration(MILLISECONDS, 1000));
        cache.withExpiryPolicy(plc2).put(key + 2, 1);
        // We should receive event about second entry expiration.
        assertNotNull(queue.poll(5, SECONDS));
    }
}
Also used : CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) BlockingArrayQueue(org.eclipse.jetty.util.BlockingArrayQueue)

Aggregations

CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)14 Duration (javax.cache.expiry.Duration)14 Ignite (org.apache.ignite.Ignite)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 AccessedExpiryPolicy (javax.cache.expiry.AccessedExpiryPolicy)2 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)2 CaffeineConfiguration (com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration)1 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)1 ICache (com.hazelcast.cache.ICache)1 ClientConfig (com.hazelcast.client.config.ClientConfig)1 XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)1 CacheConfig (com.hazelcast.config.CacheConfig)1 ClasspathXmlConfig (com.hazelcast.config.ClasspathXmlConfig)1 Config (com.hazelcast.config.Config)1 EvictionConfig (com.hazelcast.config.EvictionConfig)1 URI (java.net.URI)1