-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
if two func they have same args, but thire implements did not same. Encache return same result.
package main
import (
"fmt"
"time"
"github.com/forkbikash/encache"
)
func expensiveOperation(a, b int) (int, error) {
// Simulate an expensive operation
time.Sleep(2 * time.Second)
return a + b, nil
}
func expensiveOperation1(a, b int) (int, error) {
// Simulate an expensive operation
time.Sleep(2 * time.Second)
return a * b, nil
}
func main() {
// Create a new in-memory cache implementation
mapCache := encache.NewMapCacheImpl()
cacheKeyImpl := encache.NewDefaultCacheKeyImpl()
lockImpl := encache.NewMuLockImpl()
// Create a new encache instance
cache := encache.NewEncache(lockImpl, mapCache, cacheKeyImpl, false, time.Minute)
// Wrap the expensive function with caching
cachedExpensiveOperation := encache.CachedFunc(expensiveOperation, cache, 5*time.Minute)
cachedExpensiveOperation1 := encache.CachedFunc(expensiveOperation1, cache, 5*time.Minute)
// Call the cached function
result, err := cachedExpensiveOperation(2, 3)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", result)
// Subsequent calls will retrieve the result from the cache
result, err = cachedExpensiveOperation(2, 3)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result (cached):", result)
result, err = cachedExpensiveOperation1(2, 3)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", result)
// Subsequent calls will retrieve the result from the cache
result, err = cachedExpensiveOperation1(2, 3)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result (cached):", result)
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels