- 论坛徽章:
- 0
|
在系统中,采用内存缓存来提供系统的性能。
Cache<K, V>- package com.wms.studio.cache;
-
- import java.util.Collection;
- import java.util.Set;
-
- /**
- *
- * 缓存有效地存储临时对象,以提高一个应用的性能
- * 本系统定义一个缓存的接口,用来扩展现有的缓存框架,比如 JCache, Ehcache, JCS, OSCache, JBossCache, TerraCotta, Coherence,
- * GigaSpaces等。
- *
- */
- public interface Cache<K, V> {
-
- /**
- * 获取缓存中指定key下的值,当不存在时,返回为空
- *
- * @param 在设置缓存时指定的key
- * @return 缓存的对象或者为空
- * @throws 当访问出现问题时,抛出CacheException异常
- */
- public V get(K key) throws CacheException;
-
- /**
- * 添加一个缓存实体
- *
- * @param key 用来标识放入缓存的实体
- * @param value 存放缓存中实体的值
- * @return 返回这个key下,之前存放的实体,当不存在时,返回空
- * @throws 当访问出现问题时,抛出CacheException异常
- */
- public V put(K key, V value) throws CacheException;
-
- /**
- * 从缓存中,移除指定key的实体
- *
- * @param 在添加缓存时设置的key
- * @return 返回这个key下,之前存放的实体,当不存在时,返回空
- * @throws 当访问出现问题时,抛出CacheException异常
- */
- public V remove(K key) throws CacheException;
-
- /**
- * 清空缓存
- *
- * @throws 当访问出现问题时,抛出CacheException异常
- */
- public void clear() throws CacheException;
-
- /**
- * 返回在缓存中,存放实体的个数
- *
- * @return 在缓存中,存放实体的个数
- */
- public int size();
-
- /**
- * 返回缓存中所有实体的key
- *
- * @return 缓存中所有实体的key
- */
- public Set<K> keys();
-
- /**
- * 返回在缓存中,存放的所有实体
- *
- * @return 在缓存中,存放的所有实体
- */
- public Collection<V> values();
- }
复制代码 CacheException- /**
- * Copyright 2015 WMS(560130911@163.com)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.wms.studio.cache;
-
- /**
- * @author WMS
- * @date 2015年7月18日-下午8:57:03
- * @version 1.0
- */
- @SuppressWarnings("serial")
- public class CacheException extends RuntimeException {
-
- /**
- * 创建一个新的 <code>CacheException</code>.
- */
- public CacheException() {
- super();
- }
-
- /**
- * 创建一个新的 <code>CacheException</code>.
- *
- * @param message
- * 抛出异常的原因.
- */
- public CacheException(String message) {
- super(message);
- }
-
- /**
- * 创建一个新的 <code>CacheException</code>.
- *
- * @param cause
- * 抛出异常的底层原因.
- */
- public CacheException(Throwable cause) {
- super(cause);
- }
-
- /**
- * 创建一个新的 <code>CacheException</code>.
- *
- * @param message
- * 抛出异常的原因.
- * @param cause
- * 抛出异常的底层原因.
- */
- public CacheException(String message, Throwable cause) {
- super(message, cause);
- }
- }
复制代码 CacheManager- /**
- * Copyright 2015 WMS(560130911@163.com)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.wms.studio.cache;
-
-
- /**
- * @author WMS
- * @date 2015年7月18日-下午9:03:19
- * @version 1.0
- */
- public interface CacheManager {
-
- /**
- * 获取指定名称的缓存,当这个缓存不存在时,将会创建一个缓存并返回。
- *
- * @param name 需要获取缓存的名称
- * @return 指定名称的缓存
- * @throws 当访问出现问题时,抛出CacheException异常
- */
- public <K, V> Cache<K, V> getCache(String name) throws CacheException;
- }
复制代码 EhCache- package com.wms.studio.cache;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Set;
-
- import net.sf.ehcache.Element;
-
- import org.apache.commons.collections.CollectionUtils;
- import org.apache.log4j.Logger;
-
- public class EhCache<K, V> implements Cache<K, V> {
-
- /**
- * 私有的内部日志实例
- */
- private static final Logger log = Logger.getLogger(EhCache.class);
-
- /**
- * 包裹的Ehcache实例
- */
- private net.sf.ehcache.Ehcache cache;
-
- /**
- *
- * 使用指定net.sf.ehcache.Ehcache的实例构建一个新的EhCache实例
- *
- * @param cache - net.sf.ehcache.Ehcache实例
- */
- public EhCache(net.sf.ehcache.Ehcache cache) {
- if (cache == null) {
- throw new IllegalArgumentException("缓存参数不允许为空.");
- }
- this.cache = cache;
- }
-
- /**
- * 获取指定key下的元素
- *
- * @param key the key of the element to return.
- * @return The value placed into the cache with an earlier put, or null if not found or expired
- */
- @SuppressWarnings("unchecked")
- public V get(K key) throws CacheException {
- try {
- if (log.isTraceEnabled()) {
- log.trace("Getting object from cache [" + cache.getName() + "] for key [" + key + "]");
- }
- if (key == null) {
- return null;
- } else {
- Element element = cache.get(key);
- if (element == null) {
- if (log.isTraceEnabled()) {
- log.trace("Element for [" + key + "] is null.");
- }
- return null;
- } else {
- //noinspection unchecked
- return (V) element.getObjectValue();
- }
- }
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- /**
- * Puts an object into the cache.
- *
- * @param key the key.
- * @param value the value.
- */
- public V put(K key, V value) throws CacheException {
- if (log.isTraceEnabled()) {
- log.trace("Putting object in cache [" + cache.getName() + "] for key [" + key + "]");
- }
- try {
- V previous = get(key);
- Element element = new Element(key, value);
- cache.put(element);
- return previous;
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- /**
- * Removes the element which matches the key.
- *
- * <p>If no element matches, nothing is removed and no Exception is thrown.</p>
- *
- * @param key the key of the element to remove
- */
- public V remove(K key) throws CacheException {
- if (log.isTraceEnabled()) {
- log.trace("Removing object from cache [" + cache.getName() + "] for key [" + key + "]");
- }
- try {
- V previous = get(key);
- cache.remove(key);
- return previous;
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- /**
- * Removes all elements in the cache, but leaves the cache in a useable state.
- */
- public void clear() throws CacheException {
- if (log.isTraceEnabled()) {
- log.trace("Clearing all objects from cache [" + cache.getName() + "]");
- }
- try {
- cache.removeAll();
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- public int size() {
- try {
- return cache.getSize();
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- public Set<K> keys() {
- try {
- @SuppressWarnings({"unchecked"})
- List<K> keys = cache.getKeys();
- if (!CollectionUtils.isEmpty(keys)) {
- return Collections.unmodifiableSet(new LinkedHashSet<K>(keys));
- } else {
- return Collections.emptySet();
- }
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- public Collection<V> values() {
- try {
- @SuppressWarnings({"unchecked"})
- List<K> keys = cache.getKeys();
- if (!CollectionUtils.isEmpty(keys)) {
- List<V> values = new ArrayList<V>(keys.size());
- for (K key : keys) {
- V value = get(key);
- if (value != null) {
- values.add(value);
- }
- }
- return Collections.unmodifiableList(values);
- } else {
- return Collections.emptyList();
- }
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- /**
- * Returns the size (in bytes) that this EhCache is using in memory (RAM), or <code>-1</code> if that
- * number is unknown or cannot be calculated.
- *
- * @return the size (in bytes) that this EhCache is using in memory (RAM), or <code>-1</code> if that
- * number is unknown or cannot be calculated.
- */
- public long getMemoryUsage() {
- try {
- return cache.calculateInMemorySize();
- }
- catch (Throwable t) {
- return -1;
- }
- }
-
- /**
- * Returns the size (in bytes) that this EhCache's memory store is using (RAM), or <code>-1</code> if
- * that number is unknown or cannot be calculated.
- *
- * @return the size (in bytes) that this EhCache's memory store is using (RAM), or <code>-1</code> if
- * that number is unknown or cannot be calculated.
- */
- public long getMemoryStoreSize() {
- try {
- return cache.getMemoryStoreSize();
- }
- catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- /**
- * Returns the size (in bytes) that this EhCache's disk store is consuming or <code>-1</code> if
- * that number is unknown or cannot be calculated.
- *
- * @return the size (in bytes) that this EhCache's disk store is consuming or <code>-1</code> if
- * that number is unknown or cannot be calculated.
- */
- public long getDiskStoreSize() {
- try {
- return cache.getDiskStoreSize();
- } catch (Throwable t) {
- throw new CacheException(t);
- }
- }
-
- /**
- * Returns "EhCache [" + cache.getName() + "]"
- *
- * @return "EhCache [" + cache.getName() + "]"
- */
- public String toString() {
- return "EhCache [" + cache.getName() + "]";
- }
- }
复制代码 EhCacheManager |
|