/* * Copyright (c) Contributors, http://opensimulator.org/ * See CONTRIBUTORS.TXT for a full list of copyright holders. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the OpenSimulator Project nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.Reflection; using log4net; using Mono.Addins; using Nini.Config; using OpenSim.Framework; using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Scenes; namespace OpenSim.Region.CoreModules.Asset { /// /// Cenome memory asset cache. /// /// /// /// Cache is enabled by setting "AssetCaching" configuration to value "CenomeMemoryAssetCache". /// When cache is successfully enable log should have message /// "[ASSET CACHE]: Cenome asset cache enabled (MaxSize = XXX bytes, MaxCount = XXX, ExpirationTime = XXX)". /// /// /// Cache's size is limited by two parameters: /// maximal allowed size in bytes and maximal allowed asset count. When new asset /// is added to cache that have achieved either size or count limitation, cache /// will automatically remove less recently used assets from cache. Additionally /// asset's lifetime is controlled by expiration time. /// /// /// /// /// Configuration /// Description /// /// /// MaxSize /// Maximal size of the cache in bytes. Default value: 128MB (134 217 728 bytes). /// /// /// MaxCount /// Maximal count of assets stored to cache. Default value: 4096 assets. /// /// /// ExpirationTime /// Asset's expiration time in minutes. Default value: 30 minutes. /// /// /// /// /// /// Enabling Cenome Asset Cache: /// /// [Modules] /// AssetCaching = "CenomeMemoryAssetCache" /// /// Setting size and expiration time limitations: /// /// [AssetCache] /// ; 256 MB (default: 134217728) /// MaxSize = 268435456 /// ; How many assets it is possible to store cache (default: 4096) /// MaxCount = 16384 /// ; Expiration time - 1 hour (default: 30 minutes) /// ExpirationTime = 60 /// /// [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "CenomeMemoryAssetCache")] public class CenomeMemoryAssetCache : IAssetCache, ISharedRegionModule { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); /// /// Cache's default maximal asset count. /// /// /// /// Assuming that average asset size is about 32768 bytes. /// /// public const int DefaultMaxCount = 4096; /// /// Default maximal size of the cache in bytes /// /// /// /// 128MB = 128 * 1024^2 = 134 217 728 bytes. /// /// public const long DefaultMaxSize = 134217728; /// /// Asset's default expiration time in the cache. /// public static readonly TimeSpan DefaultExpirationTime = TimeSpan.FromMinutes(30.0); /// /// Cache object. /// private ICnmCache m_cache; /// /// Count of cache commands /// private int m_cachedCount; /// /// How many gets before dumping statistics /// /// /// If 0 or less, then disabled. /// private int m_debugEpoch; /// /// Is Cenome asset cache enabled. /// private bool m_enabled; /// /// Count of get requests /// private int m_getCount; /// /// How many hits /// private int m_hitCount; /// /// Initialize asset cache module, with custom parameters. /// /// /// Cache's maximal size in bytes. /// /// /// Cache's maximal count of assets. /// /// /// Asset's expiration time. /// protected void Initialize(long maximalSize, int maximalCount, TimeSpan expirationTime) { if (maximalSize <= 0 || maximalCount <= 0) { //m_log.Debug("[ASSET CACHE]: Cenome asset cache is not enabled."); m_enabled = false; return; } if (expirationTime <= TimeSpan.Zero) { // Disable expiration time expirationTime = TimeSpan.MaxValue; } // Create cache and add synchronization wrapper over it m_cache = CnmSynchronizedCache.Synchronized(new CnmMemoryCache( maximalSize, maximalCount, expirationTime)); m_enabled = true; m_log.DebugFormat( "[ASSET CACHE]: Cenome asset cache enabled (MaxSize = {0} bytes, MaxCount = {1}, ExpirationTime = {2})", maximalSize, maximalCount, expirationTime); } #region IAssetCache Members public bool Check(string id) { AssetBase asset; // XXX:This is probably not an efficient implementation. return m_cache.TryGetValue(id, out asset); } /// /// Cache asset. /// /// /// The asset that is being cached. /// public void Cache(AssetBase asset) { if (asset != null) { // m_log.DebugFormat("[CENOME ASSET CACHE]: Caching asset {0}", asset.ID); long size = asset.Data != null ? asset.Data.Length : 1; m_cache.Set(asset.ID, asset, size); m_cachedCount++; } } public void CacheNegative(string id) { // We don't do negative caching } /// /// Clear asset cache. /// public void Clear() { m_cache.Clear(); } /// /// Expire (remove) asset stored to cache. /// /// /// The expired asset's id. /// public void Expire(string id) { m_cache.Remove(id); } /// /// Get asset stored /// /// /// The asset's id. /// /// /// Asset if it is found from cache; otherwise . /// /// /// /// Caller should always check that is return value . /// Cache doesn't guarantee in any situation that asset is stored to it. /// /// public bool Get(string id, out AssetBase assetBase) { m_getCount++; if (m_cache.TryGetValue(id, out assetBase)) m_hitCount++; if (m_getCount == m_debugEpoch) { m_log.DebugFormat( "[ASSET CACHE]: Cached = {0}, Get = {1}, Hits = {2}%, Size = {3} bytes, Avg. A. Size = {4} bytes", m_cachedCount, m_getCount, ((double) m_hitCount / m_getCount) * 100.0, m_cache.Size, m_cache.Size / m_cache.Count); m_getCount = 0; m_hitCount = 0; m_cachedCount = 0; } // if (null == assetBase) // m_log.DebugFormat("[CENOME ASSET CACHE]: Asset {0} not in cache", id); return true; } #endregion #region ISharedRegionModule Members /// /// Gets region module's name. /// public string Name { get { return "CenomeMemoryAssetCache"; } } public Type ReplaceableInterface { get { return null; } } /// /// New region is being added to server. /// /// /// Region's scene. /// public void AddRegion(Scene scene) { if (m_enabled) scene.RegisterModuleInterface(this); } /// /// Close region module. /// public void Close() { if (m_enabled) { m_enabled = false; m_cache.Clear(); m_cache = null; } } /// /// Initialize region module. /// /// /// Configuration source. /// public void Initialise(IConfigSource source) { m_cache = null; m_enabled = false; IConfig moduleConfig = source.Configs[ "Modules" ]; if (moduleConfig == null) return; string name = moduleConfig.GetString("AssetCaching"); //m_log.DebugFormat("[XXX] name = {0} (this module's name: {1}", name, Name); if (name != Name) return; long maxSize = DefaultMaxSize; int maxCount = DefaultMaxCount; TimeSpan expirationTime = DefaultExpirationTime; IConfig assetConfig = source.Configs["AssetCache"]; if (assetConfig != null) { // Get optional configurations maxSize = assetConfig.GetLong("MaxSize", DefaultMaxSize); maxCount = assetConfig.GetInt("MaxCount", DefaultMaxCount); expirationTime = TimeSpan.FromMinutes(assetConfig.GetInt("ExpirationTime", (int)DefaultExpirationTime.TotalMinutes)); // Debugging purposes only m_debugEpoch = assetConfig.GetInt("DebugEpoch", 0); } Initialize(maxSize, maxCount, expirationTime); } /// /// Initialization post handling. /// /// /// /// Modules can use this to initialize connection with other modules. /// /// public void PostInitialise() { } /// /// Region has been loaded. /// /// /// Region's scene. /// /// /// /// This is needed for all module types. Modules will register /// Interfaces with scene in AddScene, and will also need a means /// to access interfaces registered by other modules. Without /// this extra method, a module attempting to use another modules' /// interface would be successful only depending on load order, /// which can't be depended upon, or modules would need to resort /// to ugly kludges to attempt to request interfaces when needed /// and unnecessary caching logic repeated in all modules. /// The extra function stub is just that much cleaner. /// /// public void RegionLoaded(Scene scene) { } /// /// Region is being removed. /// /// /// Region scene that is being removed. /// public void RemoveRegion(Scene scene) { } #endregion } }