From 1a6b30e0924644cb4f9b6a771d768fc7d37d7aa0 Mon Sep 17 00:00:00 2001 From: Melanie Date: Sat, 29 Aug 2009 05:54:38 +0100 Subject: [PATCH] Add a slow cache cleaner thread. By default, the thread starts a cleanup sweep every 10 minutes. If any texture data is older than 12 hours, it is regenerated and the memory cache is refreshed. After each decode, the thread delays for 5 seconds. --- .../Agent/TextureSender/J2KDecoderModule.cs | 54 ++++++++++++++++--- bin/OpenSim.ini.example | 2 +- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs b/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs index df321a2aae..937f76bb72 100644 --- a/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs +++ b/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs @@ -30,6 +30,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; +using System.Threading; using log4net; using Nini.Config; using OpenMetaverse; @@ -38,6 +39,7 @@ using OpenMetaverse.Imaging; using OpenSim.Framework; using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Scenes; +using OpenSim.Services.Interfaces; namespace OpenSim.Region.CoreModules.Agent.TextureSender { @@ -56,8 +58,11 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender private readonly Dictionary m_cacheddecode = new Dictionary(); private bool OpenJpegFail = false; private string CacheFolder = Util.dataDir() + "/j2kDecodeCache"; - private int CacheTimeout = 60; - private J2KDecodeFileCache fCache; + private int CacheTimeout = 720; + private J2KDecodeFileCache fCache = null; + private Thread CleanerThread = null; + private IAssetService AssetService = null; + private Scene m_Scene = null; /// /// List of client methods to notify of results of decode @@ -70,6 +75,9 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender public void Initialise(Scene scene, IConfigSource source) { + if (m_Scene == null) + m_Scene = scene; + IConfig j2kConfig = source.Configs["J2KDecoder"]; if (j2kConfig != null) { @@ -77,14 +85,23 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender CacheTimeout = j2kConfig.GetInt("CacheTimeout", CacheTimeout); } - fCache = new J2KDecodeFileCache(CacheFolder, CacheTimeout); + if (fCache == null) + fCache = new J2KDecodeFileCache(CacheFolder, CacheTimeout); scene.RegisterModuleInterface(this); + + if (CleanerThread == null && CacheTimeout != 0) + { + CleanerThread = new Thread(CleanCache); + CleanerThread.Name = "J2KCleanerThread"; + CleanerThread.IsBackground = true; + CleanerThread.Start(); + } } public void PostInitialise() { - + AssetService = m_Scene.AssetService; } public void Close() @@ -340,8 +357,9 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender // Cache Decoded layers lock (m_cacheddecode) { - if (!m_cacheddecode.ContainsKey(AssetId)) - m_cacheddecode.Add(AssetId, layers); + if (m_cacheddecode.ContainsKey(AssetId)) + m_cacheddecode.Remove(AssetId); + m_cacheddecode.Add(AssetId, layers); } @@ -359,6 +377,28 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender } } } + + private void CleanCache() + { + m_log.Info("[J2KDecoderModule]: Cleaner thread started"); + + while (true) + { + if (AssetService != null) + fCache.ScanCacheFiles(RedecodeTexture); + + System.Threading.Thread.Sleep(600000); + } + } + + private void RedecodeTexture(UUID assetID) + { + AssetBase texture = AssetService.Get(assetID.ToString()); + if (texture == null) + return; + + doJ2kDecode(assetID, texture.Data); + } } public class J2KDecodeFileCache @@ -656,7 +696,7 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender { File.Delete(f.Name); decode(AssetIdFromFileName(f.Name)); - System.Threading.Thread.Sleep(2000); + System.Threading.Thread.Sleep(5000); } } } diff --git a/bin/OpenSim.ini.example b/bin/OpenSim.ini.example index 3529599033..2303873dd4 100644 --- a/bin/OpenSim.ini.example +++ b/bin/OpenSim.ini.example @@ -1371,7 +1371,7 @@ [J2KDecoder] ;CacheDir = "./j2kDecodeCache" -;CacheTimeout = 60 +;CacheTimeout = 720 ;; ;; These are defaults that are overwritten below in [Architecture].