luminus-framework/expiring-map

Name: expiring-map

Owner: luminus-framework

Description: a thread-safe map that expires entries

Created: 2015-09-11 03:24:58.0

Updated: 2017-09-09 10:30:34.0

Pushed: 2017-03-30 15:16:26.0

Homepage: null

Size: 21

Language: Clojure

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

expiring-map

A Clojure wrapper for the expiringmap. The expiring-map is a thread-safe mutable map that supports expiry for elements.

Usage

Clojars Project

Basic Usage

The expiry map responds to the standard collection functions such as get, count, keys, vals, and empty?.

In addition, the map provides the following functions for mutating its values:

uire '[expiring-map.core :as em])

 cache (em/expiring-map 10))

assoc! cache :foo "bar")

 cache :bar)
 cache :foo)
 cache :bar "defualt")

nt cache)
s cache)
s cache)
ty? cache)

dissoc! cache :foo)

assoc! cache :foo "foo" :bar "bar")
reset-expiration! cache :foo)
clear! cache)
Time Units

The time unit defaults to :seconds, the following time units are supported:

e.g:

expiring-map 10 {:time-unit :minutes})
Expiration Policy

The expiration policy can either be set to creation time or last access, defaults to creation time.

 cache (em/expiring-map
        10
        {:expiration-policy :access
         :time-unit :minutes}))
Listeners

The map allows setting expiry listeners, a listener must be a function that accepts the key and the value that expires:

 cache (em/expiring-map
        10
        {:listeners [(fn [k v] (println "Expired:" k v))]}))
Criterium Benchmarks

The benchmarks below test the assoc/dissoc cycle for the expiring-map, regular atom, and core.cache atom.

atom
ING: Final GC required 409.9942905040538 % of runtime
uation count : 4756014 in 6 samples of 792669 calls.
         Execution time mean : 120.809576 ns
Execution time std-deviation : 2.684070 ns
xecution time lower quantile : 118.923613 ns ( 2.5%)
xecution time upper quantile : 124.374281 ns (97.5%)
               Overhead used : 1.988494 ns
expiring-map
ING: Final GC required 481.8947027815336 % of runtime
uation count : 1893510 in 6 samples of 315585 calls.
         Execution time mean : 322.901037 ns
Execution time std-deviation : 11.538941 ns
xecution time lower quantile : 313.718453 ns ( 2.5%)
xecution time upper quantile : 340.737836 ns (97.5%)
               Overhead used : 1.988494 ns

d 1 outliers in 6 samples (16.6667 %)
low-severe   1 (16.6667 %)
iance from outliers : 13.8889 % Variance is moderately inflated by outliers
core.cache
ING: Final GC required 398.9148954873636 % of runtime
uation count : 925734 in 6 samples of 154289 calls.
         Execution time mean : 645.750787 ns
Execution time std-deviation : 19.287433 ns
xecution time lower quantile : 625.196819 ns ( 2.5%)
xecution time upper quantile : 666.538213 ns (97.5%)
               Overhead used : 1.988494 ns
License

Copyright © 2015 Dmitri Sotnikov

Distributed under the Apache 2.0 license


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.