OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
GlynnTuckerAssetCache.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 log4net;
29 using System;
30 using System.IO;
31 using System.Collections.Generic;
32 using System.Reflection;
33 using GlynnTucker.Cache;
34 using Mono.Addins;
35 using Nini.Config;
36 using OpenSim.Framework;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
39 using OpenSim.Services.Interfaces;
40 
41 namespace OpenSim.Region.CoreModules.Asset
42 {
43  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GlynnTuckerAssetCache")]
45  {
46  private static readonly ILog m_log =
47  LogManager.GetLogger(
48  MethodBase.GetCurrentMethod().DeclaringType);
49 
50  private bool m_Enabled;
51  private ICache m_Cache;
52  private ulong m_Hits;
53  private ulong m_Requests;
54 
55  // Instrumentation
56  private uint m_DebugRate;
57 
58  public Type ReplaceableInterface
59  {
60  get { return null; }
61  }
62 
63  public string Name
64  {
65  get { return "GlynnTuckerAssetCache"; }
66  }
67 
68  public void Initialise(IConfigSource source)
69  {
70  IConfig moduleConfig = source.Configs["Modules"];
71 
72  if (moduleConfig != null)
73  {
74  string name = moduleConfig.GetString("AssetCaching");
75  //m_log.DebugFormat("[ASSET CACHE] name = {0} (this module's name: {1}). Sync? ", name, Name, m_Cache.IsSynchronized);
76 
77  if (name == Name)
78  {
79  m_Cache = new GlynnTucker.Cache.SimpleMemoryCache();
80  m_Enabled = true;
81 
82  m_log.Info("[ASSET CACHE]: GlynnTucker asset cache enabled");
83 
84  // Instrumentation
85  IConfig cacheConfig = source.Configs["AssetCache"];
86  if (cacheConfig != null)
87  m_DebugRate = (uint)cacheConfig.GetInt("DebugRate", 0);
88  }
89  }
90  }
91 
92  public void PostInitialise()
93  {
94  }
95 
96  public void Close()
97  {
98  }
99 
100  public void AddRegion(Scene scene)
101  {
102  if (m_Enabled)
103  scene.RegisterModuleInterface<IImprovedAssetCache>(this);
104  }
105 
106  public void RemoveRegion(Scene scene)
107  {
108  }
109 
110  public void RegionLoaded(Scene scene)
111  {
112  }
113 
115  // IImprovedAssetCache
116  //
117 
118  public bool Check(string id)
119  {
120  return m_Cache.Contains(id);
121  }
122 
123  public void Cache(AssetBase asset)
124  {
125  if (asset != null)
126  m_Cache.AddOrUpdate(asset.ID, asset);
127  }
128 
129  public AssetBase Get(string id)
130  {
131  Object asset = null;
132  m_Cache.TryGet(id, out asset);
133 
134  Debug(asset);
135 
136  return (AssetBase)asset;
137  }
138 
139  public void Expire(string id)
140  {
141  Object asset = null;
142  if (m_Cache.TryGet(id, out asset))
143  m_Cache.Remove(id);
144  }
145 
146  public void Clear()
147  {
148  m_Cache.Clear();
149  }
150 
151  private void Debug(Object asset)
152  {
153  // Temporary instrumentation to measure the hit/miss rate
154  if (m_DebugRate > 0)
155  {
156  ++m_Requests;
157  if (asset != null)
158  ++m_Hits;
159 
160  if ((m_Requests % m_DebugRate) == 0)
161  m_log.DebugFormat("[ASSET CACHE]: Hit Rate {0} / {1} == {2}%", m_Hits, m_Requests, ((float)m_Hits / (float)m_Requests) * 100.0f);
162  }
163  // End instrumentation
164  }
165  }
166 }
void Cache(AssetBase asset)
Cache the specified asset.
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void Expire(string id)
Expire an asset from the cache.
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
bool Check(string id)
Check whether an asset with the specified id exists in the cache.
Non-texture assets
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
AssetBase Get(string id)
Get an asset by its id.
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...