1 ///
2 module cachetools.interfaces;
3 
4 private {
5     import std.typecons;
6     import std.datetime;
7     import core.time;
8     import std.typecons;
9 }
10 
11 private import cachetools.internal;
12 
13 //
14 // cache have aspects:
15 // 1. storage: hashmap and some kind of order of elements
16 // 2. stream of evicted elements, which user may want to handle(slose files, sockets, etc)
17 // 3. eviction policy (condition to start/stop evinction)
18 //
19 
20 ///
21 enum PutResultFlag
22 {
23     None,
24     Inserted = 1 << 0,
25     Replaced = 1 << 1,
26     Evicted  = 1 << 2
27 }
28 ///
29 alias PutResult = BitFlags!PutResultFlag;
30 
31 // I failed to reach both goals: inheritance from interface and nogc/nothrow attribute neutrality
32 // for Cache implementations. So I droped inheritance.
33 //
34 //interface Cache(K, V) {
35 //
36 //    // get value from cache
37 //    Nullable!V get(K) @safe;
38 //
39 //    // put/update cache entry
40 //    PutResult put(K, V) @safe;
41 //
42 //    // remove key
43 //    bool  remove(K) @safe;
44 //    
45 //    // clear entire cache
46 //    void  clear() @safe;
47 //    
48 //    // # of elements
49 //    size_t length() const @safe;
50 //
51 //}
52 
53 enum EventType
54 {
55     Removed,
56     Expired,
57     Evicted,
58     Updated
59 }
60 
61 struct CacheEvent(K, V)
62 {
63     EventType       event;
64     StoredType!K    key;
65     StoredType!V    val;
66 }
67 
68 /**
69  * TTL encapsulate ttl for single cache item
70     1. use default - __ttl = 0
71     2. no ttl      - __ttl = -1
72     3. some value  - __ttl > 0
73  */
74 struct TTL {
75 
76     private Duration  __ttl = 0.seconds;
77 
78     ///
79     /// True if this TTL means - use default value for this cache
80     ///
81     bool useDefault() pure const nothrow @nogc @safe {
82         return __ttl == 0.seconds;
83     }
84     ///
85     /// return value encapsulated by this ttl
86     ///
87     Duration value() pure const nothrow @nogc @safe {
88         return __ttl;
89     }
90     ///
91     /// Create "no ttl" - means do not use ttl with this entry
92     ///
93     TTL opUnary(string op)() pure nothrow @safe @nogc if (op == "~")
94     {
95         return TTL(-1.seconds);
96     }
97     /**
98     / Constructor
99     / Parameters:
100     / v - ttl value (0 - use default value or no ttl if there is no defaults)
101     */
102     this(Duration v) pure nothrow @safe @nogc {
103         __ttl = v;
104     }
105     deprecated("Use TTL(Duration) instead")
106     this(int v) pure nothrow @safe @nogc
107     {
108         __ttl = v.seconds;
109     }
110 }