Skip to content
/ ttl Public
generated from devnw/oss-template

Commit 1a9db8a

Browse files
Merge pull request #6 from devnw/GH-4
Initial README documentation for the library
2 parents cd8e47f + 42433d2 commit 1a9db8a

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TTL Cache Implementation for Go
1+
# TTL (Time To Live) Cache Implementation for Go
22

33
[![Build & Test Action Status](https://github.com/devnw/ttl/actions/workflows/build.yml/badge.svg)](https://github.com/devnw/ttl/actions)
44
[![Go Report Card](https://goreportcard.com/badge/go.devnw.com/ttl)](https://goreportcard.com/report/go.devnw.com/ttl)
@@ -7,4 +7,62 @@
77
[![License: Apache 2.0](https://img.shields.io/badge/license-Apache-blue.svg)](https://opensource.org/licenses/Apache-2.0)
88
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)
99

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

Comments
 (0)