001/*******************************************************************************
002The MIT License (MIT)
003
004Copyright (c) 2024 KILLCODING.COM
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in
014all copies or substantial portions of the Software.
015
016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022THE SOFTWARE.
023*****************************************************************************/
024package com.killcoding.cache;
025
026import com.killcoding.log.Logger;
027import com.killcoding.log.LoggerFactory;
028
029import java.util.List;
030import com.killcoding.cache.AbsCacheApi;
031import com.killcoding.cache.MemoryCache;
032
033/**
034 * This class is a permanent cache storage that requires manual deletion of the cache
035 * */
036public final class StoredCache {
037
038        private static AbsCacheApi api = null;
039        private static final Logger log = LoggerFactory.getLogger(StoredCache.class);
040        
041        public synchronized static void setApi(AbsCacheApi _api){
042            api = _api;
043        }
044
045        public static AbsCacheApi getApi(){
046            return api;
047        }
048        
049        public synchronized static void set(String name,Object value){
050            if(api == null) setApi(new MemoryCache(true));
051            
052                log.debug("Set Cache: {}",name);
053                api.set(name, value, Integer.MAX_VALUE);                
054        }
055        
056        public synchronized static Object get(String name){
057            if(api == null) setApi(new MemoryCache(true));
058            
059                log.debug("Get Cache: {}",name);
060                return api.get(name);
061        }
062        
063        public synchronized static boolean included(String name){
064            if(api == null) setApi(new MemoryCache(true));
065            
066                log.debug("Included Cache: {}",name);
067                boolean b = api.included(name);
068                log.debug("Included: {}",b);
069                return b;
070        }
071        
072        public synchronized static void remove(String name){
073            if(api == null) setApi(new MemoryCache(true));
074            
075                log.debug("Remove Cache: {}",name);
076                api.remove(name);
077        }
078        
079        public synchronized static void removeAll(){
080            if(api == null) setApi(new MemoryCache(true));
081            
082                log.debug("Remove All Cache");
083                api.removeAll();
084        }
085        
086        public synchronized static List<String> getNames(){
087            if(api == null) setApi(new MemoryCache(true));
088            
089                return api.getNames();
090        }       
091        
092}