-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ashuthe1/featurepersistentStorage
Feature: Support for Saving Cache to File and vice-versa
- Loading branch information
Showing
5 changed files
with
179 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package persistence | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"os" | ||
"sync" | ||
|
||
"github.com/ashuthe1/kuki-memcache/cache" | ||
) | ||
|
||
// FilePersistence handles saving and loading data to/from a file. | ||
type FilePersistence struct { | ||
filePath string | ||
mu sync.Mutex | ||
} | ||
|
||
// NewFilePersistence creates a new FilePersistence instance with the given filePath. | ||
func NewFilePersistence(filePath string) *FilePersistence { | ||
return &FilePersistence{ | ||
filePath: filePath, | ||
} | ||
} | ||
|
||
// SaveToFile saves data to a file. | ||
func (fp *FilePersistence) SaveToFile(data interface{}) error { | ||
fp.mu.Lock() | ||
defer fp.mu.Unlock() | ||
|
||
file, err := os.Create(fp.filePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = file.Write(jsonData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// LoadFromFile loads data from a file. | ||
func (fp *FilePersistence) LoadFromFile() (map[string]cache.CacheItem, error) { | ||
fp.mu.Lock() | ||
defer fp.mu.Unlock() | ||
|
||
file, err := os.Open(fp.filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
jsonData, err := io.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data map[string]cache.CacheItem | ||
err = json.Unmarshal(jsonData, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package persistence | ||
|
||
import ( | ||
"github.com/ashuthe1/kuki-memcache/cache" | ||
) | ||
|
||
// CachePersistable defines methods that must be implemented by a cache for persistence. | ||
type CachePersistable interface { | ||
Lock() | ||
Unlock() | ||
Items() map[string]cache.CacheItem | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"key1":{"Value":"value1","ExpiresAt":"2024-07-08T19:36:19.444308625+05:30"},"key2":{"Value":"value2","ExpiresAt":"2024-07-08T19:36:19.444397391+05:30"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters