TTL Result Store
A plain capacity-bounded cache (this catalogue's MemoisationHelpers.mqh covers that case) is the right tool when a value is valid forever once computed and only gets pushed out by running low on room. It is the wrong tool when a value goes stale on its own after a while regardless of how often it is read - a quote fetched from a web request, a spread reading, a session statistic that only means something for the next few seconds or minutes. This header covers that second case: every entry carries its own expiry time, set once at Set() time from a caller-chosen time-to-live in seconds. Expiry is checked LAZILY, the same way a real cache with no background housekeeping thread behaves: Get()/Contains() check the stored expiry against the current time on every call, and silently drop an entry the moment something tries to read it past its TTL, treating it exactly like a plain cache miss. Purge() covers the other half of the picture - an eager sweep of every currently-expired entry, useful if the store is also being used to bound memory rather than relying purely on something eventually reading (and thereby discovering) each expired key. When the store is full and a genuinely new key needs a slot, the entry EXPIRING SOONEST is evicted first, not the least-recently-used one - capacity pressure and staleness are the same underlying signal here (an entry with five seconds left is closer to worthless than one with five minutes left, independent of how recently either was read), so this store deliberately does not duplicate MemoisationHelpers.mqh's LRU-clock machinery. Notes and limitations - Expiry resolution is whole seconds (TimeCurrent()-based), not milliseconds - not suitable for sub-second TTL windows. - Values are plain strings. Encode a numeric result with DoubleToString()/IntegerToString() before Set() and parse it back out after Get() - there is no typed variant. - Lookup, capacity eviction and Purge() are all linear scans over the live entries. Deliberately simple and fine for the tens-to-low- hundreds of concurrently-cached keys this header is meant for; a much larger key space would want a hash-indexed lookup and/or a min-heap keyed by expiry instead of a linear scan for the soonest- to-expire slot. - No background timer of any kind - an entry only actually leaves the store when something calls Get()/Contains() on it after it has expired, or when Purge() is called explicitly, or when capacity pressure evicts it. A store that is Set() once and never read or Purge()d again will hold onto its (already-worthless) expired entries indefinitely. - Not thread-safe and not persisted across restarts - a pure in-memory, single-terminal-instance store.
Script · MT4 · v1.0 · Free
About TTL Result Store
TTL Result Store is a script for MetaTrader 4. MQL source code included.
Files in the download: TtlResultStore.mqh, TtlResultStore.txt, TtlResultStoreDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trading Sessions · MT4 Script · MQL4 Source Code