PHP Classes

File: docs/README.md

Recommend this page to a friend!
  Packages of Dwight José Trujillo Barco   Time Travel Cache   docs/README.md   Download  
File: docs/README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Time Travel Cache
Store multiple versions of values in cache files
Author: By
Last change:
Date: 20 days ago
Size: 1,815 bytes
 

Contents

Class file image Download

?# TimeTravelCache ? Complete Documentation

Overview

TimeTravelCache stores a timestamped history of values per key. Retrieve any past version by index (0=oldest, -1=newest) or exact Unix timestamp, revert, prune old entries, and distinguish null values.

Requirements

  • PHP 7.4+

Installation

require_once 'src/TimeTravelCache.php';

Basic Usage

$cache = new TimeTravelCache(5);
$cache->set('user', 'Alice');
$cache->set('user', 'Bob');
echo $cache->get('user'); // Bob
echo $cache->getByIndex('user', -2); // Alice
$cache->revertToIndex('user', 0); // back to Alice

API Reference

Constructor

__construct(int $maxHistory = 10) ? max entries per key.

Storing & Retrieving

  • `set(string $key, mixed $value): void`
  • `get(string $key): mixed` ? latest value (null if missing or stored null)
  • `has(string $key): bool`
  • `countHistory(string $key): int`

Index Access (0 = oldest, -1 = newest)

  • `getByIndex(string $key, int $index): mixed`
  • `getEntryByIndex(string $key, int $index): ?array` ? full `['value'=>..., 'timestamp'=>...]`
  • `getLatestEntry(string $key): ?array`
  • `getEarliestEntry(string $key): ?array`

Timestamp Access

  • `getByTimestamp(string $key, int $timestamp): mixed`
  • `getEntryByTimestamp(string $key, int $timestamp): ?array`

Revert

  • `revertToIndex(string $key, int $index): bool`
  • `revertToTimestamp(string $key, int $timestamp): bool`

History Management

  • `getHistory(string $key): ?array`
  • `clearHistoryBeforeTimestamp(string $key, int $timestamp): int`
  • `clear(string $key): void`
  • `clearAll(): void`

Edge Cases

  • Storing `null` is allowed; use entry getters to differentiate from missing keys.
  • Out?of?range indices return `null` / `false`.
  • Duplicate timestamps return the first match.