@@ -57,6 +57,8 @@ func (s *memoryStorage) Exists(ctx context.Context, key string) bool {
57
57
58
58
// Store saves value at key.
59
59
func (s * memoryStorage ) Store (_ context.Context , key string , value []byte ) error {
60
+ s .mu .Lock ()
61
+ defer s .mu .Unlock ()
60
62
s .m [key ] = storageEntry {
61
63
i : KeyInfo {
62
64
Key : key ,
@@ -71,6 +73,8 @@ func (s *memoryStorage) Store(_ context.Context, key string, value []byte) error
71
73
72
74
// Load retrieves the value at key.
73
75
func (s * memoryStorage ) Load (_ context.Context , key string ) ([]byte , error ) {
76
+ s .mu .Lock ()
77
+ defer s .mu .Unlock ()
74
78
val , ok := s .m [key ]
75
79
if ! ok {
76
80
return nil , os .ErrNotExist
@@ -80,12 +84,20 @@ func (s *memoryStorage) Load(_ context.Context, key string) ([]byte, error) {
80
84
81
85
// Delete deletes the value at key.
82
86
func (s * memoryStorage ) Delete (_ context.Context , key string ) error {
87
+ s .mu .Lock ()
88
+ defer s .mu .Unlock ()
83
89
delete (s .m , key )
84
90
return nil
85
91
}
86
92
87
93
// List returns all keys that match prefix.
88
94
func (s * memoryStorage ) List (ctx context.Context , prefix string , recursive bool ) ([]string , error ) {
95
+ s .mu .Lock ()
96
+ defer s .mu .Unlock ()
97
+ return s .list (ctx , prefix , recursive )
98
+ }
99
+
100
+ func (s * memoryStorage ) list (ctx context.Context , prefix string , recursive bool ) ([]string , error ) {
89
101
var keyList []string
90
102
91
103
keys := make ([]string , 0 , len (s .m ))
@@ -104,7 +116,7 @@ func (s *memoryStorage) List(ctx context.Context, prefix string, recursive bool)
104
116
// If current key is a directory
105
117
if recursive && k != trimmedKey {
106
118
// Recursively traverse all child directories
107
- childKeys , err := s .List (ctx , fullPathKey , recursive )
119
+ childKeys , err := s .list (ctx , fullPathKey , recursive )
108
120
if err != nil {
109
121
return keyList , err
110
122
}
@@ -119,6 +131,8 @@ func (s *memoryStorage) List(ctx context.Context, prefix string, recursive bool)
119
131
120
132
// Stat returns information about key.
121
133
func (s * memoryStorage ) Stat (_ context.Context , key string ) (KeyInfo , error ) {
134
+ s .mu .Lock ()
135
+ defer s .mu .Unlock ()
122
136
val , ok := s .m [key ]
123
137
if ! ok {
124
138
return KeyInfo {}, os .ErrNotExist
0 commit comments