|
1 | | -# TTL Cache Implementation for Go |
| 1 | +# TTL (Time To Live) Cache Implementation for Go |
2 | 2 |
|
3 | 3 | [](https://github.com/devnw/ttl/actions) |
4 | 4 | [](https://goreportcard.com/report/go.devnw.com/ttl) |
|
7 | 7 | [](https://opensource.org/licenses/Apache-2.0) |
8 | 8 | [](http://makeapullrequest.com) |
9 | 9 |
|
10 | | -To use `go get -u go.devnw.com/ttl@latest` |
| 10 | +## Overview |
| 11 | + |
| 12 | +A TTL (Time To Live) cache is a data store where data is removed after a defined period of time to optimize memory performance. |
| 13 | + |
| 14 | +### Implementation |
| 15 | + |
| 16 | +#### TTL Details |
| 17 | + |
| 18 | +This TTL Cache implementation is designed for ease of use. There are two methods for setting TTL for a data element. |
| 19 | + |
| 20 | +1. TTL is configured when the Cache is instantiated using `ttl.NewCache` which accepts a `time.Duration` parameter for the default TTL of the cache instance. Unless otherwise specified for a specific data element, this is the TTL for all data stored in the cache. |
| 21 | + |
| 22 | +2. TTL for each data element can be configured using the `cache.SetTTL` meethod which accepts a `time.Duration` which will set a specific TTL for that piece of data when it's loaded into the cache. |
| 23 | + |
| 24 | +#### TTL Extension |
| 25 | + |
| 26 | +In order to optimize data access and retention an `extension` option was included as part of the implementation of the TTL cache. This can be enabled as part of the `NewCache` method. TTL Extension configures the cache so that when data is `READ` from the cache the timeout for that **key/value** pair is reset extending the life of that data in memory. This allows for regularly accessed data to be retained while less regularly accessed data is cleared from memory. |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +`go get -u go.devnw.com/ttl@latest` |
| 31 | + |
| 32 | +### Cache Creation |
| 33 | + |
| 34 | +```go |
| 35 | +cache := ttl.NewCache( |
| 36 | + ctx, // Context used in the application |
| 37 | + timeout, // `time.Duration` that configures the default timeout for elements of the cache |
| 38 | + extend, // boolean which configures whether or not the timeout should be reset on READ |
| 39 | +) |
| 40 | +``` |
| 41 | + |
| 42 | +The `NewCache` method returns the `ttl.Cache` interface which defines the expected API for storing and accessing data in the cache. |
| 43 | + |
| 44 | +### Add Data to Cache |
| 45 | + |
| 46 | +Adding to the cache uses a simple key/value pattern for setting data. There are two functions for adding data to the cache. The standard method `Set` uses the cache's configured default timeout. |
| 47 | + |
| 48 | +`err := cache.Set(ctx, key, value)` |
| 49 | + |
| 50 | +The `SetTTL` method uses the timeout (`time.Duration`) that is passed into the method for configuring how long the cache should hold the data before it's cleared from memory. |
| 51 | + |
| 52 | +`err := cache.SetTTL(ctx, key, value, timeout)` |
| 53 | + |
| 54 | +### Get Data from Cache |
| 55 | + |
| 56 | +Getting data from the cache follows a fairly standard pattern which is similar ot the `sync.Map` get method. |
| 57 | + |
| 58 | +`value, exists := cache.Get(ctx, key)` |
| 59 | + |
| 60 | +The Get method returns the value (if it exists) and a boolean which indicates if a value was retrieved from the cache. |
| 61 | + |
| 62 | +### Delete Data from Cache |
| 63 | + |
| 64 | +As with Get, Delete uses a similar pattern to `sync.Map`. |
| 65 | + |
| 66 | +`cache.Delete(ctx, key)` |
| 67 | + |
| 68 | +This deletes the key from the map as well as shuts down the backend routines running that key's processing. |
0 commit comments