Package net.sf.cache4j

Examples of net.sf.cache4j.CacheFactory


     * {@link net.sf.cache4j.CacheFactory}.
     * @param in �������� ����� � XML �������������
     * @throws CacheException ���� ������� ������ � ������������
     */
    public static void loadConfig(InputStream in) throws CacheException {
        CacheFactory cf = CacheFactory.getInstance();

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(in);

            NodeList nodeList = document.getChildNodes();
            Node node = nodeList==null || nodeList.getLength()==0 ? null : nodeList.item(0);

            //�������� ���� ������ ���������� cache-config
            if (node==null || !"cache-factory".equalsIgnoreCase(node.getNodeName())) {
                throw new CacheException("root node must be \"cache-factory\"");
            }

            if ((node instanceof Element)) {
                long cleanInteval = getTimeLong(((Element)node).getAttribute("clean-interval"));
                if(cleanInteval>0){
                    cf.setCleanInterval(cleanInteval);
                } else {
                    //�� ��������� 30 ������
                    cf.setCleanInterval(30000); //30sec
                }
            }

            for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
                if ((n instanceof Element) && "cache".equalsIgnoreCase(n.getNodeName())) {
                    Cache cache = null;
                    CacheConfig config = null;

                    String id = ((Element)n).getAttribute("id");
                    String desc = ((Element)n).getAttribute("desc");
                    long ttl = getTimeLong(((Element)n).getAttribute("ttl"));
                    long idle = getTimeLong(((Element)n).getAttribute("idle"));
                    long maxMemorySize = getCapacityLong(((Element)n).getAttribute("max-memory-size"));
                    int maxSize = getInt(((Element)n).getAttribute("max-size"));

                    String type = ((Element)n).getAttribute("type");
                    if(type==null || type.trim().length()==0){
                        type = "synchronized";
                    }
                    type = type.trim().toLowerCase();
                    if(type.equals("blocking")){
                        cache = new BlockingCache();
                    } else if(type.equals("synchronized")) {
                        cache = new SynchronizedCache();
                    } else if(type.equals("nocache")) {
                        cache = new EmptyCache();
                    } else {
                        throw new CacheException("Unknown cache type:"+type);
                    }

                    String algorithm = ((Element)n).getAttribute("algorithm");
                    if(algorithm==null || algorithm.trim().length()==0){
                        algorithm = "lru";
                    }
                    algorithm = algorithm.trim().toLowerCase();
                    if(!algorithm.equals(CacheConfigImpl.LRU) &&
                       !algorithm.equals(CacheConfigImpl.LFU) &&
                       !algorithm.equals(CacheConfigImpl.FIFO) ) {
                        throw new CacheException("Unknown cache algorithm:"+algorithm);
                    }

                    String reference = ((Element)n).getAttribute("reference");
                    if(reference==null || reference.trim().length()==0){
                        reference = "strong";
                    }
                    reference = reference.trim().toLowerCase();
                    if(!reference.equals("strong") && !reference.equals("soft") ) {
                        throw new CacheException("Unknown cache object reference:"+reference);
                    }

                    config = new CacheConfigImpl(id, desc, ttl, idle, maxMemorySize, maxSize, type, algorithm, reference);
                    ((ManagedCache)cache).setCacheConfig(config);

                    cf.addCache(cache);
                }
            }

        } catch (SAXParseException e) {
            String msg = "Parsing error, line " + e.getLineNumber() + ", uri " + e.getSystemId()+"\n"+
View Full Code Here


     * ���������� ����� ���������� ������� ��������� ������
     * @throws Exception
     */
    public void afterEachMethod() throws Exception {
        //������� CacheFactory
        CacheFactory cf = CacheFactory.getInstance();
        Object[] cacheIds = cf.getCacheIds();
        for (int i = 0, indx = cacheIds==null ? 0 : cacheIds.length; i <indx; i++) {
            cf.removeCache(cacheIds[i]);
        }
    }
View Full Code Here

     * ����� �����. ��������� ������� � ������������ �������� ����� �,
     * ������������� �������� ������� X*2, �������� ������� ����� �� �*3.
     * � ���������� ��� ������ ���� �������� ������.
     */
    public static boolean test_CACHE_CLEANER_TTL() throws Exception {
        CacheFactory cf = CacheFactory.getInstance();
        long ttl = 100;
        long cleanInterval = ttl*2;
        long sleep = ttl*3;

        SynchronizedCache cache = new SynchronizedCache();
        CacheConfig cacheConfig = new CacheConfigImpl("cacheId", null, ttl, 0, 0, 0, null, "lru", "strong");
        cache.setCacheConfig(cacheConfig);

        for (int i = 0; i <1000; i++) {
            cache.put(new Long(i), new Long(i));
        }

        cf.addCache(cache);
        cf.setCleanInterval(cleanInterval);
        Thread.sleep(sleep);

        return cache.size()==0 ? true : false;
    }
View Full Code Here

     * ����� �����������. ��������� ������� � ������������ �������� ����������� �,
     * ������������� �������� ������� X*2, �������� ������� ����� �� �*3.
     * � ���������� ��� ������ ���� �������� ������.
     */
    public static boolean test_CACHE_CLEANER_IDLE() throws Exception {
        CacheFactory cf = CacheFactory.getInstance();
        long idle = 100;
        long cleanInterval = idle*2;
        long sleep = idle*3;

        SynchronizedCache cache = new SynchronizedCache();
        CacheConfig cacheConfig = new CacheConfigImpl("cacheId", null, 0, idle, 0, 0, null, "lru", "strong");
        cache.setCacheConfig(cacheConfig);

        for (int i = 0; i <1000; i++) {
            cache.put(new Long(i), new Long(i));
        }

        cf.addCache(cache);
        cf.setCleanInterval(cleanInterval);
        Thread.sleep(sleep);

        return cache.size()==0 ? true : false;
    }
View Full Code Here

     * ���������� ����� ���������� ������� ��������� ������
     * @throws java.lang.Exception
     */
    public void afterEachMethod() throws Exception {
        //������� CacheFactory
        CacheFactory cf = CacheFactory.getInstance();
        Object[] cacheIds = cf.getCacheIds();
        for (int i = 0, indx = cacheIds==null ? 0 : cacheIds.length; i <indx; i++) {
            cf.removeCache(cacheIds[i]);
        }
    }
View Full Code Here

     * ����� �����. ��������� ������� � ������������ �������� ����� �,
     * ������������� �������� ������� X*2, �������� ������� ����� �� �*3.
     * � ���������� ��� ������ ���� �������� ������.
     */
    public static boolean test_CACHE_CLEANER_TTL() throws Exception {
        CacheFactory cf = CacheFactory.getInstance();
        long ttl = 100;
        long cleanInterval = ttl*2;
        long sleep = ttl*3;

        BlockingCache cache = new BlockingCache();
        CacheConfig cacheConfig = new CacheConfigImpl("cacheId", null, ttl, 0, 0, 0, null, "lru", "strong");
        cache.setCacheConfig(cacheConfig);

        for (int i = 0; i <1000; i++) {
            cache.put(new Long(i), new Long(i));
        }

        cf.addCache(cache);
        cf.setCleanInterval(cleanInterval);
        Thread.sleep(sleep);

        return cache.size()==0 ? true : false;
    }
View Full Code Here

     * ����� �����������. ��������� ������� � ������������ �������� ����������� �,
     * ������������� �������� ������� X*2, �������� ������� ����� �� �*3.
     * � ���������� ��� ������ ���� �������� ������.
     */
    public static boolean test_CACHE_CLEANER_IDLE() throws Exception {
        CacheFactory cf = CacheFactory.getInstance();
        long idle = 100;
        long cleanInterval = idle*2;
        long sleep = idle*3;

        BlockingCache cache = new BlockingCache();
        CacheConfig cacheConfig = new CacheConfigImpl("cacheId", null, 0, idle, 0, 0, null, "lru", "strong");
        cache.setCacheConfig(cacheConfig);

        for (int i = 0; i <1000; i++) {
            cache.put(new Long(i), new Long(i));
        }

        cf.addCache(cache);
        cf.setCleanInterval(cleanInterval);
        Thread.sleep(sleep);

        return cache.size()==0 ? true : false;
    }
View Full Code Here

TOP

Related Classes of net.sf.cache4j.CacheFactory

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.