Skip to content

two func have same args with same result #1

@lorentz-wu

Description

@lorentz-wu

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)

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions