Package javax.cache

Examples of javax.cache.Cache


    @Test
    public void defaultCacheTest() {
        CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
        assertNotNull(cacheManager);

        Cache testCache = cacheManager.getCache("default");
        assertNull(testCache);
        Caching.getCachingProvider().close();
    }
View Full Code Here


    //create the cache
    cacheManager.createCache(cacheName, config);

    //... and then later to get the cache
    Cache cache = cacheManager.getCache(cacheName);

    //use the cache
    String key = "key";
    Integer value1 = 1;
    cache.put(key, value1);
    //wrong
    cache.put(value1, key);
    Integer value2 = (Integer) cache.get(key);
    assertEquals(value1, value2);

    cache.remove(key);
    assertNull(cache.get(key));
  }
View Full Code Here

    simpleCache.put("key1", 3);
    Integer value2 = simpleCache.get("key1");

    //Shows how you might try to get around runtime+generics safety
    Cache secondReferenceToCache = simpleCache;

    try {
      secondReferenceToCache.put(123, "String");
    } catch (ClassCastException e) {
      //But the RI is an implementation that performs runtime enforcement and throws
      //a ClassCastException. Implementations may perform runtime enforcement.
    }
  }
View Full Code Here

        String[] split = execute.split(",");
        if (split.length != 3)
            throw new IllegalArgumentException("Illegal execute args: " + execute);

        String name = split[0];
        Cache cache = cacheConfig.findCache(name);
        if (cache != null) {
            String op = split[1];
            Object key;
            try {
                key = Long.parseLong(split[2]);
            } catch (Throwable ignored) {
                key = split[2];
            }

            // ops
            if ("get".equals(op)) {
                display(key, cache.get(key));
            } else if ("getCacheEntry".equals(op)) {
                display(key, cache.getCacheEntry(key));
            } else if ("peek".equals(op)) {
                display(key, cache.peek(key));
            } else if ("remove".equals(op)) {
                display(key, cache.remove(key));
            } else if ("size".equals(op)) {
                display("Size: ", cache.size());
            }
        } else {
            display("", "No such cache:" + name);
        }
    }
View Full Code Here

        String[] split = execute.split(",");
        if (split.length != 2)
            throw new IllegalArgumentException("Illegal stats args: " + execute);

        String name = split[0];
        Cache cache = cacheConfig.findCache(name);
        if (cache != null) {
            CacheStatistics stats = cache.getCacheStatistics();
            String op = split[1];
            Method m = CacheStatistics.class.getMethod(op);
            display(op, m.invoke(stats));
        } else {
            display("", "No such cache:" + name);
View Full Code Here

        // sanity check
        if (cacheable == null)
            throw new IllegalArgumentException("Null cachable, invalid usage?");

        String cacheName = cacheable.name();
        Cache cache = cacheConfig.configureCache(cacheName);

        CacheMode mode = cacheable.mode(); // should not be null, as we got intercepted

        Class<? extends KeyStrategy> ksClass = cacheable.key();
        KeyStrategy ks = keys.get(ksClass);
        if (ks == null) {
            try {
                ks = ksClass.newInstance();
            } catch (Exception e) {
                log.fine("Error creating KeyStrategy: " + e);

                Constructor<? extends KeyStrategy> ctor = ksClass.getConstructor(CacheConfig.class);
                ks = ctor.newInstance(cacheConfig);
            }
            keys.put(ksClass, ks);
        }

        Object target = ctx.getTarget();
        Object[] args = ctx.getParameters();
        Serializable key = ks.createKey(target, m, args);

        Object value = null;

        try {
            if (mode == CacheMode.READ_ONLY || mode == CacheMode.ALL)
                value = cache.get(key);

            if (value != null) {
                Object unwraped = ks.unwrap(value, target, m, args);
                if (unwraped != null) // could be invalidated
                    return unwraped;
            }

            value = ctx.proceed();

            if (value != null && (mode == CacheMode.WRITE_ONLY || mode == CacheMode.ALL))
                cache.put(key, ks.wrap(value, target, m, args));
            else if (mode == CacheMode.REMOVE)
                cache.remove(key);
            else if (mode == CacheMode.EVICT)
                cache.evict();
            else if (mode == CacheMode.CLEAR)
                cache.clear();

            return value;
        } catch (Throwable e) {
            return exceptionHandler.handleException(cache, ctx, key, value, e);
        }
View Full Code Here

        return manager.getCache(name);
    }

    @SuppressWarnings({"unchecked"})
    public Cache configureCache(String name) throws CacheException {
        Cache cache = manager.getCache(name);
        if (cache != null)
            return cache;

        Map config = createConfig(name);
        Map wrappedConfig = new HashMap(config);
View Full Code Here

        manager.registerCache(name, cache);
        return cache;
    }

    public boolean evictCache(String name) {
        Cache cache = manager.getCache(name);
        if (cache != null) {
            cache.evict();
            return true;
        }
        return false;
    }
View Full Code Here

        }
        return false;
    }

    public boolean clearCache(String name) {
        Cache cache = manager.getCache(name);
        if (cache != null) {
            cache.clear();
            return true;
        }
        return false;
    }
View Full Code Here

            if (cacheName == null)
                throw new CacheException("Missing JPA entity cache name.");
        }

        Cache cache = configureCache(cacheName);
        return factory.createCacheEntryLookup(cacheName, cache);
    }
View Full Code Here

TOP

Related Classes of javax.cache.Cache

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.