Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kostya/memory_cache
Super simple in memory key-value storage with expires for Crystal.
https://github.com/kostya/memory_cache
Last synced: 4 days ago
JSON representation
Super simple in memory key-value storage with expires for Crystal.
- Host: GitHub
- URL: https://github.com/kostya/memory_cache
- Owner: kostya
- License: mit
- Created: 2016-01-31T02:32:19.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-03-28T20:29:56.000Z (over 3 years ago)
- Last Synced: 2024-11-02T13:34:04.490Z (11 days ago)
- Language: Crystal
- Homepage:
- Size: 80.1 KB
- Stars: 29
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MemoryCache
[![CI](https://github.com/kostya/memory_cache/workflows/CI/badge.svg)](https://github.com/kostya/memory_cache/actions?query=workflow%3ACI)
Super simple in memory key-value storage with expires for Crystal.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
memory_cache:
github: kostya/memory_cache
```## Usage
```crystal
require "memory_cache"cache = MemoryCache(String, Int32).new
cache.write("bla", 1)
p cache.read("bla") # => 1cache.fetch("haha") { 2 }
p cache.read("haha") # => 2cache.write("expired1", 1, expires_in: 1.second)
p cache.read("expired1") # => 1
sleep 1
p cache.read("expired1") # => nilp cache.fetch("expired1", expires_in: 1.second) { 2 } # => 2
p cache.fetch("expired1", expires_in: 1.second) { 3 } # => 2
sleep 1
p cache.fetch("expired1", expires_in: 1.second) { 3 } # => 3
```