Skip to content

Commit 17f98a6

Browse files
committed
Update README.
1 parent 063a35c commit 17f98a6

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,77 @@
1-
# periodic
1+
# Periodic
2+
[![Maven](https://img.shields.io/maven-central/v/ca.dvgi/periodic_2.13?color=blue)](https://search.maven.org/search?q=g:ca.dvgi%20periodic) [![CI](https://img.shields.io/github/actions/workflow/status/dvgica/periodic/ci.yml?branch=main)](https://github.com/dvgica/periodic/actions)
3+
4+
Periodic is a Scala library providing a variable that self-updates on a periodic basis.
5+
6+
- [Motivation](#motivation)
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [Contributing](#contributing)
10+
11+
## Motivation
12+
13+
This library is useful for caching semi-static data in memory and having that data automatically updated periodically. The source of the data is typically another process that can be queried to get a new data value. If the cached data becomes stale at a predictable interval, the cached data can be updated before this occurs. If the cached data becomes stale at unpredictable times, the stale data must still be usable. Concrete use cases include:
14+
15+
- caching a time-limited key or token, and replacing it with a new one before it expires (e.g. an OAuth access token)
16+
- caching data that changes irregularly and occasionally, such as a list of a country's airports and their codes
17+
18+
## Installation
19+
20+
Periodic is available on Maven Central for Scala 2.12, 2.13, and 3. Java 11+ is required.
21+
22+
There is currently a `periodic-api` project which exposes an interface for `AutoUpdatingVar`, and a `periodic-jdk` project which implements the API using primitives included in the JDK. Other implementations may follow.
23+
24+
If a specific implementation is required, add the following dependency to your project:
25+
26+
`"ca.dvgi" %% "periodic-jdk" % "<latest>"`
27+
28+
If only the interface is required, add:
29+
30+
`"ca.dvgi" %% "periodic-api" % "<latest>"`
31+
32+
## Usage
33+
34+
``` scala
35+
import ca.dvgi.periodic.jdk._
36+
import ca.dvgi.periodic._
37+
import scala.concurrent.duration._
38+
import scala.concurrent.Await
39+
import java.time.Instant
40+
41+
def updateData(): String = Instant.now.toString
42+
43+
val data = new JdkAutoUpdatingVar(
44+
updateData(),
45+
// can also be dynamic based on the last data
46+
UpdateInterval.Static(1.second),
47+
// can also be finite with configurable behavior for attempt exhaustion
48+
UpdateAttemptStrategy.Infinite(5.seconds),
49+
)
50+
51+
// `ready` returns a `Future[Unit]` which completes when the initial data initialization is complete
52+
// see also the `blockUntilReadyTimeout` parameter
53+
Await.result(data.ready, 5.seconds)
54+
55+
// the data is cached in-memory and can be access using `latest`
56+
println(s"Cached data is ${data.latest}")
57+
// data has not been updated yet, same result
58+
println(s"Cached data is still ${data.latest}")
59+
60+
Thread.sleep(2000)
61+
62+
// the `AutoUpdatingVar` fetched new data while this thread was sleeping
63+
println(s"New cached data is ${data.latest}")
64+
```
65+
66+
This returns something like:
67+
```
68+
Cached data is 2023-10-19T02:35:22.467418Z
69+
Cached data is still 2023-10-19T02:35:22.467418Z
70+
New cached data is 2023-10-19T02:35:23.474155Z
71+
```
72+
73+
For handling errors during update, and other options, see the Scaladocs.
74+
75+
## Contributing
76+
77+
Contributions in the form of Issues and PRs are welcome.

0 commit comments

Comments
 (0)