001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.cache; 020 021import org.apache.shiro.lang.util.SoftHashMap; 022 023/** 024 * Simple memory-only based {@link CacheManager CacheManager} implementation usable in production 025 * environments. It will not cause memory leaks as it produces {@link Cache Cache}s backed by 026 * {@link SoftHashMap SoftHashMap}s which auto-size themselves based on the runtime environment's memory 027 * limitations and garbage collection behavior. 028 * <p/> 029 * While the {@code Cache} instances created are thread-safe, they do not offer any enterprise-level features such as 030 * cache coherency, optimistic locking, failover or other similar features. For more enterprise features, consider 031 * using a different {@code CacheManager} implementation backed by an enterprise-grade caching product (Hazelcast, 032 * EhCache, TerraCotta, Coherence, GigaSpaces, etc., etc.). 033 * 034 * @since 1.0 035 */ 036public class MemoryConstrainedCacheManager extends AbstractCacheManager { 037 038 /** 039 * Returns a new {@link MapCache MapCache} instance backed by a {@link SoftHashMap}. 040 * 041 * @param name the name of the cache 042 * @return a new {@link MapCache MapCache} instance backed by a {@link SoftHashMap}. 043 */ 044 @Override 045 protected <K, V> Cache<K, V> createCache(String name) { 046 return new MapCache<>(name, new SoftHashMap<>()); 047 } 048}