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;
030
031/**
032 * This class is Cache entry
033 * */
034public final class Cache {
035    
036    /**
037     * Default is use mode 'MemoryCache'
038     * */
039        private static AbsCacheApi api = null;
040        private static final Logger log = LoggerFactory.getLogger(Cache.class);
041        
042        /**
043         * Set cache mode 
044         * @param _api - It is class 'MemoryCache' or 'DiskCache'
045         * */
046    public synchronized static void setApi(AbsCacheApi _api){
047        api = _api;
048    }
049  
050        /**
051         * Set detect cache timer (Default is 100MS)
052         * @param timer - It is detect cache timer
053         * */   
054    public static void setTimer(long timer) {
055        AbsCacheApi.TIMER = timer;
056    }
057
058    /**
059     * Set a cache
060     * @param name - It is cache key name
061     * @param value - It is cache value
062     * @param life - It is life length seconds
063     * */
064        public synchronized static void set(String name,Object value,int life){
065            if(api == null) setApi(new MemoryCache());
066            
067                if(life > 0){
068                log.debug("Set Cache: {}",name);
069                api.set(name, value, life);     
070                }
071        }
072        
073    /**
074     * Set a cache
075     * @param name - It is cache key name
076     * @param value - It is cache value
077     * */
078        public synchronized static void set(String name,Object value){
079            if(api == null) setApi(new MemoryCache());
080            
081                log.debug("Set Cache: {}",name);
082                api.set(name, value, Integer.MAX_VALUE);        
083        }       
084        
085        /**
086         * Get a cache value
087         * @return Object - It is cache value
088         * @param name - It is cache key name
089         * */
090        public synchronized static Object get(String name){
091            if(api == null) setApi(new MemoryCache());
092            
093                log.debug("Get Cache: {}",name);
094                return api.get(name);
095        }
096        
097        /**
098         * Check is included cache key
099         * @return boolean - 'true' is included, 'false' is not included.
100         * @param name - It is cache key name
101         * */
102        public synchronized static boolean included(String name){
103            if(api == null) setApi(new MemoryCache());
104            
105                log.debug("Included Cache: {}",name);
106                boolean b = api.included(name);
107                log.debug("Included: {}",b);
108                return b;
109        }
110        
111        /**
112         * Remove a cache
113         * @param name - It is cache key name
114         * */
115        public synchronized static void remove(String name){
116            if(api == null) setApi(new MemoryCache());
117            
118                log.debug("Remove Cache: {}",name);
119                api.remove(name);
120        }
121        
122        /**
123         * Remove all cache
124         * */
125        public synchronized static void removeAll(){
126                log.debug("Remove All Cache");
127                api.removeAll();
128        }
129
130        /**
131         * Get all cache key name list
132         * @return List<String> - It is key name list
133         * */   
134        public synchronized static List<String> getNames(){
135                return api.getNames();
136        }       
137
138}