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 * Get a cache value 075 * @return Object - It is cache value 076 * @param name - It is cache key name 077 * */ 078 public synchronized static Object get(String name){ 079 if(api == null) setApi(new MemoryCache()); 080 081 log.debug("Get Cache: {}",name); 082 return api.get(name); 083 } 084 085 /** 086 * Check is included cache key 087 * @return boolean - 'true' is included, 'false' is not included. 088 * @param name - It is cache key name 089 * */ 090 public synchronized static boolean included(String name){ 091 if(api == null) setApi(new MemoryCache()); 092 093 log.debug("Included Cache: {}",name); 094 boolean b = api.included(name); 095 log.debug("Included: {}",b); 096 return b; 097 } 098 099 /** 100 * Remove a cache 101 * @param name - It is cache key name 102 * */ 103 public synchronized static void remove(String name){ 104 if(api == null) setApi(new MemoryCache()); 105 106 log.debug("Remove Cache: {}",name); 107 api.remove(name); 108 } 109 110 /** 111 * Remove all cache 112 * */ 113 public synchronized static void removeAll(){ 114 log.debug("Remove All Cache"); 115 api.removeAll(); 116 } 117 118 /** 119 * Get all cache key name list 120 * @return List<String> - It is key name list 121 * */ 122 public synchronized static List<String> getNames(){ 123 return api.getNames(); 124 } 125 126}