Skip to content

Instantly share code, notes, and snippets.

@gippy
Last active January 2, 2018 11:20
Show Gist options
  • Save gippy/f734dbdbdfb85f97c5e46241c22eb09c to your computer and use it in GitHub Desktop.
Save gippy/f734dbdbdfb85f97c5e46241c22eb09c to your computer and use it in GitHub Desktop.
Interface for our redis buffer
class Buffer {
push(store, record){
in a single transaction
add record to buffer - RPUSH
if this is first write into this store then save timestamp - SETNX
extract keys from the object and add them to set - SADD
increment size of the data in buffer - INCRBY
get size, number of records, unique keys - GET, LLEN, SMEMBERS
get timestamp of frist write - GET
return size, count, keys and timestamp
}
prepareToPersist(store){
get current timestamp
in a single transaction
prepend all store keys with the timestamp - RENAMENX
remove lock if it exists - DEL
get buffer contents and unique keys - LRANGE, SMEMBERS
return timestamp, data and keys
}
getContents(store, timestamp?){
if timestamp is provided then prepend it to all keys
in a single pipeline
get buffer contents and unique keys - LRANGE, SMEMBERS
return data and keys
}
empty(store, timestamp?){
if timestamp is provided then prepend it to all keys
in a single operation remove all keys for this store - DEL
}
lock(duration, store, timestamp?){
if timestamp is provided then prepend it to lock key
attempt to get value of lock key - GET
if it exists return false to signal that lock failed
in a single transation
attempt to get value of preexisting key - GET
create key with expiration set to provided duration - SETEX
if the GET operation returned value then
return false to signal that lock operation failed
else
return true to signal that lock was successful
}
getOld(){
get all ids of stores in buffer - KEYS
get timestamps of first write operations - GET
return list of stores with timestamps
}
getPrepared(){
get all stores in buffer with keys prepended with timestamp - KEYS
return list of stores with persist operation timestamps
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment