Skip to content

Latest commit

 

History

History

examples

Moka Examples

This directory contains examples of how to use Moka cache. Each example is a standalone binary that can be run with the following command:

$ cargo run --example <example_name> -F sync,future

Each example has a suffix _async or _sync:

  • _async indicates that the example uses the moka::future::Cache, which is a Future-aware, concurrent cache.
  • _sync indicates that the example uses the moka::sync::Cache, which is a multi-thread safe, concurrent cache.

Basics of the Cache API

  • basics_async and basics_sync

    • Shares a cache between async tasks or OS threads.
      • Do not wrap a Cache with Arc<Mutex<_>>! Just clone the Cache and you are all set.
    • Uses insert, get and invalidate methods.
  • size_aware_eviction_sync

    • Configures the max capacity of the cache based on the total size of the cached entries.

The Entry API

Atomically inserts, updates and removes an entry from the cache depending on the existence of the entry.

Expiration and Eviction Listener

  • eviction_listener_sync

    • Configures the time_to_live expiration policy.
    • Registers a listener (closure) to be notified when an entry is evicted from the cache.
    • Uses insert, invalidate, invalidate_all and run_pending_tasks methods.
    • Demonstrates when the expired entries will be actually evicted from the cache, and why the run_pending_tasks method could be important in some cases.
  • cascading_drop_async

    • Controls the lifetime of the objects in a separate BTreeMap collection from the cache using an eviction listener.
    • Beside the cache APIs, uses BTreeMap, Arc and mpsc channel (multi-producer, single consumer channel).
  • reinsert_expired_entries_sync

    • Reinserts the expired entries into the cache using eviction listener and worker threads.
    • Spawns two worker threads; one for reinserting entries, and the other for calling run_pending_tasks.
    • Uses a mpsc channel (multi-producer, single consumer channel) to send commands from the eviction listener to the first worker thread.

Check out the API Documentation too!

The examples are not meant to be exhaustive. Please check the API documentation for more examples and details.