001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.utils; 017 018import com.jfinal.plugin.ehcache.IDataLoader; 019import io.jboot.Jboot; 020import io.jboot.components.cache.JbootCache; 021import io.jboot.components.cache.JbootCacheManager; 022 023import java.util.List; 024 025/** 026 * Usage: 027 * 1、CacheUtil.get("cacheName","key") 028 * 2、CacheUtil.use("default").get("cacheName","key") 029 */ 030public class CacheUtil { 031 032 public static JbootCache use(String name) { 033 return JbootCacheManager.me().getCache(name); 034 } 035 036 public static JbootCache setThreadCacheNamePrefix(String cacheNamePrefix) { 037 return JbootCacheManager.me().getCache().setThreadCacheNamePrefix(cacheNamePrefix); 038 } 039 040 public static void clearThreadCacheNamePrefix() { 041 JbootCacheManager.me().getCache().clearThreadCacheNamePrefix(); 042 } 043 044 public static <T> T get(String cacheName, Object key) { 045 return Jboot.getCache().get(cacheName, key); 046 } 047 048 public static void put(String cacheName, Object key, Object value) { 049 Jboot.getCache().put(cacheName, key, value); 050 } 051 052 public static void put(String cacheName, Object key, Object value, int liveSeconds) { 053 Jboot.getCache().put(cacheName, key, value, liveSeconds); 054 } 055 056 public static List getKeys(String cacheName) { 057 return Jboot.getCache().getKeys(cacheName); 058 } 059 060 public static void remove(String cacheName, Object key) { 061 Jboot.getCache().remove(cacheName, key); 062 } 063 064 public static void removeAll(String cacheName) { 065 Jboot.getCache().removeAll(cacheName); 066 } 067 068 public static <T> T get(String cacheName, Object key, IDataLoader dataLoader) { 069 return Jboot.getCache().get(cacheName, key, dataLoader); 070 } 071 072 public static <T> T get(String cacheName, Object key, IDataLoader dataLoader, int liveSeconds) { 073 return Jboot.getCache().get(cacheName, key, dataLoader, liveSeconds); 074 } 075 076 public static Integer getTtl(String cacheName, Object key) { 077 return Jboot.getCache().getTtl(cacheName, key); 078 } 079 080 public static void setTtl(String cacheName, Object key, int seconds) { 081 Jboot.getCache().setTtl(cacheName, key, seconds); 082 } 083}