summaryrefslogtreecommitdiffhomepage
path: root/storage/memory.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/memory.go')
-rw-r--r--storage/memory.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/storage/memory.go b/storage/memory.go
new file mode 100644
index 0000000..80f6579
--- /dev/null
+++ b/storage/memory.go
@@ -0,0 +1,47 @@
+package storage
+
+var _ CHR = &Memory{}
+
+// Memory is an in-memory non-persistent storage type
+// using it in production is not recommended
+type Memory struct {
+ store map[string]string
+}
+
+// Create will store a value in a key in memory
+func (r *Memory) Create(key, value string, checkcollision bool) error {
+ if !r.Healthy() {
+ return Unhealthy
+ }
+ if checkcollision {
+ if _, ok := r.store[key]; ok {
+ return Collision
+ }
+ }
+ r.store[key] = value
+ return nil
+}
+
+// Read will read a key from memory, if it's there
+func (r *Memory) Read(key string) (string, error) {
+ if !r.Healthy() {
+ return "", Unhealthy
+ }
+ if val, ok := r.store[key]; ok {
+ return val, nil
+ }
+ return "", Error("value not found")
+}
+
+// Healthy checks if the memory storage is initialized
+func (r *Memory) Healthy() bool {
+ return r.store != nil
+}
+
+// NewMemory initializes a memory backend for use
+func NewMemory() CHR {
+ m := Memory{
+ store: make(map[string]string),
+ }
+ return &m
+}