Loading…

Two Tier Memory File Cache

A plain in-memory cache with a capacity limit simply loses data once it is full and something new needs a slot - fine for a pure speed-up cache, wrong for anything an EA would rather not lose entirely (a computed indicator-value cache, a per-symbol session-statistics table). A write-through cache solves the loss problem by writing every single Set() straight to disk, but then pays a file write on every call whether or not that particular entry is ever actually evicted - expensive for a working set that gets updated far more often than it is displaced. This header keeps a small capacity-bounded memory tier and spills the least-recently-used entry to a file only when the memory tier is full and a new key needs a slot. Writes (Set()) only ever touch memory, marking the entry dirty; a value reaches disk on two occasions only - LRU eviction, or an explicit FlushDirty() call - never synchronously on every Set(). That is what "write-back" means here, as distinct from "write-through": the expensive disk write is batched to the entries that actually get displaced or that the caller explicitly asks to persist, not paid on every update regardless. Get() checks memory first, then the file-based spill tier via an in- memory offset index (no linear file scan). A spill-tier hit is PROMOTED back into memory - clean, since it already matches what is on disk - so a key that keeps getting re-read after being spilled does not keep paying the file-read cost on every access; only the first read after a spill touches disk. Notes and limitations - The spill file is append-only internally: a superseded or promoted entry's old on-disk bytes are simply orphaned, only the in-memory index entry pointing at them is dropped. The file grows with total churn, not with the number of currently-cached keys - it is not compacted. Re-Open() (which truncates) periodically if long-running churn is expected to matter. - A crash between a dirty Set() and the next eviction/FlushDirty() loses that write - this is a cache with a write-back policy, not a crash-safe journal (see AppendOnlyJournal.mqh elsewhere in this catalogue if durability of every individual write matters more than cache speed). - Values are stored and read back as plain strings via StringToCharArray/CharArrayToString - suitable for text, numbers formatted to string, or short serialised payloads; not for raw binary data containing embedded null bytes. - Both memory-tier lookup and the spill index use linear search over their respective key lists. This is deliberately simple and fine for the capacity sizes (tens to low hundreds of entries) this header is meant for; a much larger cache would want a hash-indexed lookup instead. - Not thread-safe / not safe across multiple terminals writing the SAME file concurrently - exactly one CTwoTierCache instance should own a given filename at a time.

Script · MT4 · v1.0 · Free

About Two Tier Memory File Cache

Two Tier Memory File Cache is a script for MetaTrader 4. MQL source code included.

Files in the download: TwoTierCache.mqh, TwoTierCache.txt, TwoTierCacheDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Trading Sessions · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria