OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
CenomeAssetCache.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Reflection;
30 using log4net;
31 using Mono.Addins;
32 using Nini.Config;
33 using OpenSim.Framework;
34 using OpenSim.Region.Framework.Interfaces;
35 using OpenSim.Region.Framework.Scenes;
36 
37 namespace OpenSim.Region.CoreModules.Asset
38 {
93  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "CenomeMemoryAssetCache")]
95  {
96  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
97 
106  public const int DefaultMaxCount = 4096;
107 
116  public const long DefaultMaxSize = 134217728;
117 
121  public static readonly TimeSpan DefaultExpirationTime = TimeSpan.FromMinutes(30.0);
122 
126  private ICnmCache<string, AssetBase> m_cache;
127 
131  private int m_cachedCount;
132 
139  private int m_debugEpoch;
140 
144  private bool m_enabled;
145 
149  private int m_getCount;
150 
154  private int m_hitCount;
155 
168  protected void Initialize(long maximalSize, int maximalCount, TimeSpan expirationTime)
169  {
170  if (maximalSize <= 0 || maximalCount <= 0)
171  {
172  //m_log.Debug("[ASSET CACHE]: Cenome asset cache is not enabled.");
173  m_enabled = false;
174  return;
175  }
176 
177  if (expirationTime <= TimeSpan.Zero)
178  {
179  // Disable expiration time
180  expirationTime = TimeSpan.MaxValue;
181  }
182 
183  // Create cache and add synchronization wrapper over it
184  m_cache =
185  CnmSynchronizedCache<string, AssetBase>.Synchronized(new CnmMemoryCache<string, AssetBase>(
186  maximalSize, maximalCount, expirationTime));
187  m_enabled = true;
188  m_log.DebugFormat(
189  "[ASSET CACHE]: Cenome asset cache enabled (MaxSize = {0} bytes, MaxCount = {1}, ExpirationTime = {2})",
190  maximalSize,
191  maximalCount,
192  expirationTime);
193  }
194 
195  #region IImprovedAssetCache Members
196 
197  public bool Check(string id)
198  {
199  AssetBase asset;
200 
201  // XXX:This is probably not an efficient implementation.
202  return m_cache.TryGetValue(id, out asset);
203  }
204 
211  public void Cache(AssetBase asset)
212  {
213  if (asset != null)
214  {
215 // m_log.DebugFormat("[CENOME ASSET CACHE]: Caching asset {0}", asset.ID);
216 
217  long size = asset.Data != null ? asset.Data.Length : 1;
218  m_cache.Set(asset.ID, asset, size);
219  m_cachedCount++;
220  }
221 
222  }
223 
227  public void Clear()
228  {
229  m_cache.Clear();
230  }
231 
238  public void Expire(string id)
239  {
240  m_cache.Remove(id);
241  }
242 
258  public AssetBase Get(string id)
259  {
260  m_getCount++;
261  AssetBase assetBase;
262  if (m_cache.TryGetValue(id, out assetBase))
263  m_hitCount++;
264 
265  if (m_getCount == m_debugEpoch)
266  {
267  m_log.DebugFormat(
268  "[ASSET CACHE]: Cached = {0}, Get = {1}, Hits = {2}%, Size = {3} bytes, Avg. A. Size = {4} bytes",
269  m_cachedCount,
270  m_getCount,
271  ((double) m_hitCount / m_getCount) * 100.0,
272  m_cache.Size,
273  m_cache.Size / m_cache.Count);
274  m_getCount = 0;
275  m_hitCount = 0;
276  m_cachedCount = 0;
277  }
278 
279 // if (null == assetBase)
280 // m_log.DebugFormat("[CENOME ASSET CACHE]: Asset {0} not in cache", id);
281 
282  return assetBase;
283  }
284 
285  #endregion
286 
287  #region ISharedRegionModule Members
288 
292  public string Name
293  {
294  get { return "CenomeMemoryAssetCache"; }
295  }
296 
297  public Type ReplaceableInterface
298  {
299  get { return null; }
300  }
301 
308  public void AddRegion(Scene scene)
309  {
310  if (m_enabled)
311  scene.RegisterModuleInterface<IImprovedAssetCache>(this);
312  }
313 
317  public void Close()
318  {
319  if (m_enabled)
320  {
321  m_enabled = false;
322  m_cache.Clear();
323  m_cache = null;
324  }
325  }
326 
333  public void Initialise(IConfigSource source)
334  {
335  m_cache = null;
336  m_enabled = false;
337 
338  IConfig moduleConfig = source.Configs[ "Modules" ];
339  if (moduleConfig == null)
340  return;
341 
342  string name = moduleConfig.GetString("AssetCaching");
343  //m_log.DebugFormat("[XXX] name = {0} (this module's name: {1}", name, Name);
344 
345  if (name != Name)
346  return;
347 
348  long maxSize = DefaultMaxSize;
349  int maxCount = DefaultMaxCount;
350  TimeSpan expirationTime = DefaultExpirationTime;
351 
352  IConfig assetConfig = source.Configs["AssetCache"];
353  if (assetConfig != null)
354  {
355  // Get optional configurations
356  maxSize = assetConfig.GetLong("MaxSize", DefaultMaxSize);
357  maxCount = assetConfig.GetInt("MaxCount", DefaultMaxCount);
358  expirationTime =
359  TimeSpan.FromMinutes(assetConfig.GetInt("ExpirationTime", (int)DefaultExpirationTime.TotalMinutes));
360 
361  // Debugging purposes only
362  m_debugEpoch = assetConfig.GetInt("DebugEpoch", 0);
363  }
364 
365  Initialize(maxSize, maxCount, expirationTime);
366  }
367 
376  public void PostInitialise()
377  {
378  }
379 
399  public void RegionLoaded(Scene scene)
400  {
401  }
402 
409  public void RemoveRegion(Scene scene)
410  {
411  }
412 
413  #endregion
414  }
415 }
void PostInitialise()
Initialization post handling.
void Expire(string id)
Expire (remove) asset stored to cache.
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
void Initialize(long maximalSize, int maximalCount, TimeSpan expirationTime)
Initialize asset cache module, with custom parameters.
bool Check(string id)
Check whether an asset with the specified id exists in the cache.
Non-texture assets
void Initialise(IConfigSource source)
Initialize region module.
void RemoveRegion(Scene scene)
Region is being removed.
void AddRegion(Scene scene)
New region is being added to server.
Interactive OpenSim region server
Definition: OpenSim.cs:55
void RegionLoaded(Scene scene)
Region has been loaded.