@ehmpathy/simple-dynamodb-cache
v1.0.4
Published
A simple dynamodb cache with time based expiration policies
Downloads
17
Maintainers
Readme
simple-dynamodb-cache
A simple dynamodb cache with time based expiration policies
features
- simplicity:
get
andset
with an intuitive, pit-of-success, design - interoperability: fulfills the standard SimpleAsyncCache interface
- can be used with
- garbage collection: uses dynamodb ttl to automatically garbage collect stale entries
install
npm install simple-dynamodb-cache
use
create a dynamodb table
as you'd expect, this cache persists to dynamodb, so you'll need to define a table it can use
this library expects that the dynamodb table it'll be given has the following attributes
- partitionKey =
p
- ttlKey =
t
here's a terraform example for creating a table with this schema
resource "aws_dynamodb_table" "table_domain_driven_cache" {
name = "infrastructure-${var.environment}-table-domain-driven-cache"
billing_mode = "PAY_PER_REQUEST"
point_in_time_recovery {
enabled = var.environment == "prod" ? true : false
}
hash_key = "p" # partition key
ttl {
enabled = true
attribute_name = "t" # ttl key
}
attribute {
name = "p"
type = "S"
}
}
create a cache
const cache = createCache({ table: 'svc-raindrops-prod-cache' });
set to the cache
await cache.set('answer', '42');
ℹ️ note: if you'd like an item to never expire, set the expiration time to Infinity
get from the cache
await cache.get('answer'); // '42'