001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 * Copyright (c) 2016, Lucee Assosication Switzerland
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either 
009 * version 2.1 of the License, or (at your option) any later version.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public 
017 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
018 * 
019 **/
020package lucee.runtime.type.scope.client;
021
022import java.util.Date;
023
024import lucee.commons.io.cache.CacheEntry;
025import lucee.commons.io.log.Log;
026import lucee.commons.lang.Pair;
027import lucee.runtime.PageContext;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.type.Collection;
030import lucee.runtime.type.Struct;
031import lucee.runtime.type.StructImpl;
032import lucee.runtime.type.scope.Client;
033import lucee.runtime.type.scope.session.SessionCache;
034import lucee.runtime.type.scope.storage.StorageScopeCache;
035import lucee.runtime.type.scope.storage.StorageValue;
036
037public final class ClientCache extends StorageScopeCache implements Client {
038        
039        private static final long serialVersionUID = -875719423763891692L;
040        
041        private ClientCache(PageContext pc,String cacheName, String appName,Struct sct, long lastStored) { 
042                super(pc,cacheName,appName,"client",SCOPE_CLIENT,sct,lastStored);
043        }
044
045        /**
046         * Constructor of the class, clone existing
047         * @param other
048         */
049        private ClientCache(StorageScopeCache other,boolean deepCopy) {
050                super(other,deepCopy);
051        }
052        
053
054        
055        @Override
056        public Collection duplicate(boolean deepCopy) {
057        return new ClientCache(this,deepCopy);
058        }
059        
060        /**
061         * load an new instance of the client datasource scope
062         * @param cacheName 
063         * @param appName
064         * @param pc
065         * @param log 
066         * @return client datasource scope
067         * @throws PageException
068         */
069        public static synchronized Client getInstance(String cacheName, String appName, PageContext pc, Client existing, Log log) throws PageException {
070                StorageValue sv = _loadData(pc, cacheName, appName,"client", log);
071                if(appName!=null && appName.startsWith("no-in-memory-cache-")) existing=null;
072                if(sv!=null) {
073                        long time = sv.lastModified();
074
075                        if(existing instanceof StorageScopeCache) {
076                                if(((StorageScopeCache)existing).lastModified()>=time)
077                                        return existing;
078                        }
079                        return new ClientCache(pc,cacheName,appName,sv.getValue(),time);
080                }
081                else if(existing!=null) return  existing;
082                
083                ClientCache cc = new ClientCache(pc,cacheName,appName,new StructImpl(),0);
084                cc.store(pc.getConfig());
085                return cc;
086        }
087        
088
089        public static Client getInstance(String cacheName, String appName, PageContext pc, Client existing, Log log,Client defaultValue) {
090                try {
091                        return getInstance(cacheName, appName, pc,existing,log);
092                }
093                catch (PageException e) {}
094                return defaultValue;
095        }
096
097}