rename to WithCacheStore
This commit is contained in:
parent
cfcfe7cb21
commit
c4751467f5
5 changed files with 24 additions and 26 deletions
|
|
@ -36,8 +36,8 @@ type GetElementFuncT4WithKey[K comparable, T any, T2 any, T3 any, T4 any] func(T
|
||||||
// CacheOption defines a function that configures a CachedNode.
|
// CacheOption defines a function that configures a CachedNode.
|
||||||
type CacheOption func(*CachedNode)
|
type CacheOption func(*CachedNode)
|
||||||
|
|
||||||
// WithStore allows providing a custom cache implementation for a cached component.
|
// WithCacheStore allows providing a custom cache implementation for a cached component.
|
||||||
func WithStore(store cache.Store[any, string]) CacheOption {
|
func WithCacheStore(store cache.Store[any, string]) CacheOption {
|
||||||
return func(c *CachedNode) {
|
return func(c *CachedNode) {
|
||||||
c.cache = store
|
c.cache = store
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
framework/h/cache/README.md
vendored
6
framework/h/cache/README.md
vendored
|
|
@ -69,7 +69,7 @@ UserProfile := h.CachedPerKeyT(
|
||||||
|
|
||||||
### Using a Custom Cache
|
### Using a Custom Cache
|
||||||
|
|
||||||
You can provide your own cache implementation using the `WithStore` option:
|
You can provide your own cache implementation using the `WithCacheStore` option:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
@ -92,7 +92,7 @@ var (
|
||||||
return h.Div(h.Text("User profile"))
|
return h.Div(h.Text("User profile"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
h.WithStore(lruCache), // Pass the custom cache
|
h.WithCacheStore(lruCache), // Pass the custom cache
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
@ -240,7 +240,7 @@ cache := cache.NewLRUStore[any, string](100_000)
|
||||||
UserContent := h.CachedPerKey(
|
UserContent := h.CachedPerKey(
|
||||||
5*time.Minute,
|
5*time.Minute,
|
||||||
getUserContent,
|
getUserContent,
|
||||||
h.WithStore(cache),
|
h.WithCacheStore(cache),
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
16
framework/h/cache/example_test.go
vendored
16
framework/h/cache/example_test.go
vendored
|
|
@ -81,7 +81,7 @@ func ExampleCachedPerKeyT() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example demonstrates using a memory-bounded LRU cache
|
// Example demonstrates using a memory-bounded LRU cache
|
||||||
func ExampleWithStore_lru() {
|
func ExampleWithCacheStore_lru() {
|
||||||
// Create an LRU cache that holds maximum 1000 items
|
// Create an LRU cache that holds maximum 1000 items
|
||||||
lruStore := cache.NewLRUStore[any, string](1000)
|
lruStore := cache.NewLRUStore[any, string](1000)
|
||||||
defer lruStore.Close()
|
defer lruStore.Close()
|
||||||
|
|
@ -100,7 +100,7 @@ func ExampleWithStore_lru() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
h.WithStore(lruStore), // Use custom cache store
|
h.WithCacheStore(lruStore), // Use custom cache store
|
||||||
)
|
)
|
||||||
|
|
||||||
// Render many products
|
// Render many products
|
||||||
|
|
@ -142,8 +142,6 @@ func (a *DistributedCacheAdapter) Set(key any, value string, ttl time.Duration)
|
||||||
a.cache.data[keyStr] = value
|
a.cache.data[keyStr] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func (a *DistributedCacheAdapter) Delete(key any) {
|
func (a *DistributedCacheAdapter) Delete(key any) {
|
||||||
a.cache.mutex.Lock()
|
a.cache.mutex.Lock()
|
||||||
defer a.cache.mutex.Unlock()
|
defer a.cache.mutex.Unlock()
|
||||||
|
|
@ -164,19 +162,19 @@ func (a *DistributedCacheAdapter) Close() {
|
||||||
func (a *DistributedCacheAdapter) GetOrCompute(key any, compute func() string, ttl time.Duration) string {
|
func (a *DistributedCacheAdapter) GetOrCompute(key any, compute func() string, ttl time.Duration) string {
|
||||||
a.cache.mutex.Lock()
|
a.cache.mutex.Lock()
|
||||||
defer a.cache.mutex.Unlock()
|
defer a.cache.mutex.Unlock()
|
||||||
|
|
||||||
keyStr := fmt.Sprintf("htmgo:%v", key)
|
keyStr := fmt.Sprintf("htmgo:%v", key)
|
||||||
|
|
||||||
// Check if exists
|
// Check if exists
|
||||||
if val, ok := a.cache.data[keyStr]; ok {
|
if val, ok := a.cache.data[keyStr]; ok {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute and store
|
// Compute and store
|
||||||
value := compute()
|
value := compute()
|
||||||
a.cache.data[keyStr] = value
|
a.cache.data[keyStr] = value
|
||||||
// In a real implementation, you'd also set TTL in Redis
|
// In a real implementation, you'd also set TTL in Redis
|
||||||
|
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -192,7 +190,7 @@ func ExampleDistributedCacheAdapter() {
|
||||||
// Use it with a cached component
|
// Use it with a cached component
|
||||||
SharedComponent := h.Cached(10*time.Minute, func() *h.Element {
|
SharedComponent := h.Cached(10*time.Minute, func() *h.Element {
|
||||||
return h.Div(h.Text("Shared across all servers"))
|
return h.Div(h.Text("Shared across all servers"))
|
||||||
}, h.WithStore(adapter))
|
}, h.WithCacheStore(adapter))
|
||||||
|
|
||||||
html := h.Render(SharedComponent())
|
html := h.Render(SharedComponent())
|
||||||
fmt.Printf("Cached in distributed store: %v\n", len(distCache.data) > 0)
|
fmt.Printf("Cached in distributed store: %v\n", len(distCache.data) > 0)
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ func TestCached_WithCustomStore(t *testing.T) {
|
||||||
CachedDiv := Cached(1*time.Hour, func() *Element {
|
CachedDiv := Cached(1*time.Hour, func() *Element {
|
||||||
callCount++
|
callCount++
|
||||||
return Div(Text(fmt.Sprintf("Rendered %d times", callCount)))
|
return Div(Text(fmt.Sprintf("Rendered %d times", callCount)))
|
||||||
}, WithStore(lruStore))
|
}, WithCacheStore(lruStore))
|
||||||
|
|
||||||
// First render
|
// First render
|
||||||
html1 := Render(CachedDiv())
|
html1 := Render(CachedDiv())
|
||||||
|
|
@ -115,7 +115,7 @@ func TestCachedPerKey_WithLRUStore(t *testing.T) {
|
||||||
renderCounts[userID]++
|
renderCounts[userID]++
|
||||||
return Div(Text(fmt.Sprintf("User %d", userID)))
|
return Div(Text(fmt.Sprintf("User %d", userID)))
|
||||||
}
|
}
|
||||||
}, WithStore(lruStore))
|
}, WithCacheStore(lruStore))
|
||||||
|
|
||||||
// Render 2 users - fill cache to capacity
|
// Render 2 users - fill cache to capacity
|
||||||
Render(UserProfile(1))
|
Render(UserProfile(1))
|
||||||
|
|
@ -214,7 +214,7 @@ func TestCachedPerKeyT_WithCustomStore(t *testing.T) {
|
||||||
P(Text(a.Content)),
|
P(Text(a.Content)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}, WithStore(ttlStore))
|
}, WithCacheStore(ttlStore))
|
||||||
|
|
||||||
article1 := Article{ID: 1, Title: "First", Content: "Content 1"}
|
article1 := Article{ID: 1, Title: "First", Content: "Content 1"}
|
||||||
article2 := Article{ID: 2, Title: "Second", Content: "Content 2"}
|
article2 := Article{ID: 2, Title: "Second", Content: "Content 2"}
|
||||||
|
|
@ -275,7 +275,7 @@ func TestCachedPerKey_ConcurrentAccess(t *testing.T) {
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
return Div(Text(fmt.Sprintf("User %d", userID)))
|
return Div(Text(fmt.Sprintf("User %d", userID)))
|
||||||
}
|
}
|
||||||
}, WithStore(lruStore))
|
}, WithCacheStore(lruStore))
|
||||||
|
|
||||||
const numGoroutines = 50
|
const numGoroutines = 50
|
||||||
const numUsers = 20
|
const numUsers = 20
|
||||||
|
|
@ -419,7 +419,7 @@ func TestCachedNode_ClearCache(t *testing.T) {
|
||||||
CachedDiv := Cached(1*time.Hour, func() *Element {
|
CachedDiv := Cached(1*time.Hour, func() *Element {
|
||||||
callCount++
|
callCount++
|
||||||
return Div(Text("Content"))
|
return Div(Text("Content"))
|
||||||
}, WithStore(lruStore))
|
}, WithCacheStore(lruStore))
|
||||||
|
|
||||||
// Render and cache
|
// Render and cache
|
||||||
element := CachedDiv()
|
element := CachedDiv()
|
||||||
|
|
|
||||||
|
|
@ -188,7 +188,7 @@ lruCache := cache.NewLRUStore[string, string](10000) // Max 10k items
|
||||||
var CachedUserProfile = h.CachedPerKeyT(
|
var CachedUserProfile = h.CachedPerKeyT(
|
||||||
15*time.Minute,
|
15*time.Minute,
|
||||||
getUserProfile,
|
getUserProfile,
|
||||||
h.WithStore(lruCache), // Pass the custom store
|
h.WithCacheStore(lruCache), // Pass the custom store
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@ -300,7 +300,7 @@ redisCache := NewRedisStore[string, string](
|
||||||
var CachedUserData = h.CachedPerKeyT(
|
var CachedUserData = h.CachedPerKeyT(
|
||||||
15*time.Minute,
|
15*time.Minute,
|
||||||
getUserData,
|
getUserData,
|
||||||
h.WithStore(redisCache),
|
h.WithCacheStore(redisCache),
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@ -312,7 +312,7 @@ ttlCache := cache.NewTTLStore[string, string]()
|
||||||
var CachedData = h.Cached(
|
var CachedData = h.Cached(
|
||||||
5*time.Minute,
|
5*time.Minute,
|
||||||
getData,
|
getData,
|
||||||
h.WithStore(ttlCache),
|
h.WithCacheStore(ttlCache),
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@ -328,7 +328,7 @@ var CachedUserProfile = h.CachedPerKeyT(
|
||||||
return renderUserProfile(userID)
|
return renderUserProfile(userID)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
h.WithStore(lruCache),
|
h.WithCacheStore(lruCache),
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@ -351,13 +351,13 @@ memoryCache := cache.NewLRUStore[any, string](10000)
|
||||||
|
|
||||||
var CachedDashboard = h.Cached(10*time.Minute, func() *h.Element {
|
var CachedDashboard = h.Cached(10*time.Minute, func() *h.Element {
|
||||||
return renderDashboard()
|
return renderDashboard()
|
||||||
}, h.WithStore(memoryCache))
|
}, h.WithCacheStore(memoryCache))
|
||||||
|
|
||||||
var CachedUserData = h.CachedPerKeyT(15*time.Minute, func(userID string) (string, h.GetElementFunc) {
|
var CachedUserData = h.CachedPerKeyT(15*time.Minute, func(userID string) (string, h.GetElementFunc) {
|
||||||
return userID, func() *h.Element {
|
return userID, func() *h.Element {
|
||||||
return renderUserData(userID)
|
return renderUserData(userID)
|
||||||
}
|
}
|
||||||
}, h.WithStore(memoryCache))
|
}, h.WithCacheStore(memoryCache))
|
||||||
`
|
`
|
||||||
|
|
||||||
const DistributedCacheExample = `
|
const DistributedCacheExample = `
|
||||||
|
|
@ -395,7 +395,7 @@ var CachedSearchResults = h.CachedPerKeyT(
|
||||||
return performSearch(normalized)
|
return performSearch(normalized)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
h.WithStore(boundedCache),
|
h.WithCacheStore(boundedCache),
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue