From the course: Advanced Go Programming: Data Structures, Code Architecture, and Testing

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Solution: In-memory cache

Solution: In-memory cache

(upbeat music) - [Instructor] I hope you had fun experimenting and implementing this challenge. Let's have a look at my solution for the in-memory cache. On lines 27 to 30, I've made some changes to our Cache custom type by adding a values map using the key and value types defined on lines 16 and 17. I've also added a ReadWriteMutex to the cache in order to make it safe for concurrent usage. On line 34, I modified the implementation of the new cache function to initialize the values map. We don't need to initialize the ReadWriteMutex because the zero values of the locks in the sync package are ready for use. On lines 38 to 42, I implemented the set method and began by acquiring the cache lock. Then immediately deferring the unlock. Since this method will write to the values map, I acquired the full lock. Then on line 41, I wrote the new value together with its corresponding key. The values map will take care of value…

Contents