@@ -23,6 +23,9 @@ import (
2323
2424var memCache sync.Map
2525
26+ //var memoryCache = make(map[string][]byte)
27+ //var mutex = &sync.RWMutex{}
28+
2629// DefineHostFuncCacheSet defines a new Go module function for setting values in
2730// the cache. It takes in 6 parameters:
2831// - key position (int32)
@@ -73,6 +76,13 @@ var cacheSet = api.GoModuleFunc(func(ctx context.Context, module api.Module, par
7376
7477 // start the host work
7578 memCache .Store (string (bufferKey ), bufferStringValue )
79+
80+ /*
81+ mutex.Lock()
82+ defer mutex.Unlock()
83+ memoryCache[string(bufferKey)] = bufferStringValue
84+ */
85+
7686 resultFromHost = success (bufferKey )
7787 //! we cannot know if there is an error or not
7888 // end of the host work
@@ -88,6 +98,7 @@ var cacheSet = api.GoModuleFunc(func(ctx context.Context, module api.Module, par
8898
8999 params [0 ] = 0
90100
101+
91102})
92103
93104
@@ -125,14 +136,28 @@ var cacheGet = api.GoModuleFunc(func(ctx context.Context, module api.Module, par
125136 // Execute the host function with the arguments and return a value
126137 var resultFromHost []byte
127138
139+ /*
140+ mutex.RLock()
141+ defer mutex.RUnlock()
142+ result := memoryCache[string(bufferKey)]
143+ if result == nil {
144+ resultFromHost = failure([]byte("key not found"))
145+ } else {
146+ resultFromHost = success(result)
147+ }
148+ */
149+
150+
128151 // start the host work
152+
129153 result , ok := memCache .Load (string (bufferKey ))
130154
131155 if ok {
132156 resultFromHost = success (result .([]byte ))
133157 } else {
134158 resultFromHost = failure ([]byte ("key not found" ))
135159 }
160+
136161 // end of the host work
137162
138163 // return the result value (using the return buffer)
0 commit comments