Created
February 10, 2023 11:19
-
-
Save ronaldwolvers/40eedee22fc538582177a6802f1cd880 to your computer and use it in GitHub Desktop.
Generic paged cache for Go maps (Go version 1.19)
This file contains hidden or 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
| package caching | |
| type pagedMapCacheKey struct { | |
| key int | |
| page_number int | |
| page_size int | |
| } | |
| const enable_caching bool = false | |
| type pagedMap[V any] map[pagedMapCacheKey]*V | |
| var paged_cache interface{} | |
| func GetCacheValuePaged[V any](key int, page_number int, page_size int) *V { | |
| if !enable_caching { | |
| return nil | |
| } | |
| if paged_cache == nil { | |
| return nil | |
| } | |
| vMap, _ := paged_cache.(pagedMap[V]) | |
| if vMap != nil { | |
| return vMap[pagedMapCacheKey{key, page_number, page_size}] | |
| } else { | |
| return nil | |
| } | |
| } | |
| func SetCacheValue[V any](key int, page_number int, page_size int, value *V) { | |
| if paged_cache == nil { | |
| paged_cache = make(pagedMap[V]) | |
| } | |
| vMap, _ := paged_cache.(pagedMap[V]) | |
| if vMap != nil { | |
| vMap[pagedMapCacheKey{key, page_number, page_size}] = value | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment