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