From 8ad9e862e2f1da57b0a68cf3785bdee68859a323 Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz> Date: Sun, 14 Aug 2022 21:55:25 +0200 Subject: [PATCH] Wrappers: Cache --- Wrappers.cs | 37 +++++++++++++++++++++++++++++++++++++ sample.conf | 5 +++++ 2 files changed, 42 insertions(+) diff --git a/Wrappers.cs b/Wrappers.cs index e24681f..822e8f0 100644 --- a/Wrappers.cs +++ b/Wrappers.cs @@ -68,6 +68,9 @@ abstract partial class StatusBar } } + if(section.Contains("_cache")) + module = new WrapperCache(module, section.Mandatory("_cache").AsMs()); + module.Init(parrent, section); return module; } @@ -145,3 +148,37 @@ class WrapperExec: ModuleWrapper } }).ToArray(); } + +class WrapperCache: ModuleWrapper +{ + IEnumerable<Block> data; + long? dataTime_ms; + long maxOld_ms; + bool wantRedraw = false; + public WrapperCache(Module _child, long _maxOld_ms):base(_child) + { + maxOld_ms = _maxOld_ms; + } + override public IEnumerable<Block> Get() + { + lock(this) + if(wantRedraw) + { + data = null; + dataTime_ms = null; + } + child.Get(); + if(dataTime_ms == null || dataTime_ms + maxOld_ms <= Environment.TickCount64) + { + dataTime_ms = Environment.TickCount64; + data = child.Get(); + } + return data; + } + override public void Schedule(int in_ms) + { + lock(this) + wantRedraw = true; + parent.Schedule(in_ms); + } +} diff --git a/sample.conf b/sample.conf index 4e9906b..b9edc21 100644 --- a/sample.conf +++ b/sample.conf @@ -311,6 +311,11 @@ _exec right super shift = osdc "Super+Shift+Right pressed" # <string> # Command to run +_cache = 10 +# <time> +# Fetch data from module only once per this time. +# Note that visible data couldn't be more than (_cache + refresh) old. + # Parsers: # ======== -- GitLab