rename to WithCacheStore

This commit is contained in:
maddalax 2025-07-03 13:56:36 -05:00
parent cfcfe7cb21
commit c4751467f5
5 changed files with 24 additions and 26 deletions

View file

@ -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.
type CacheOption func(*CachedNode)
// WithStore allows providing a custom cache implementation for a cached component.
func WithStore(store cache.Store[any, string]) CacheOption {
// WithCacheStore allows providing a custom cache implementation for a cached component.
func WithCacheStore(store cache.Store[any, string]) CacheOption {
return func(c *CachedNode) {
c.cache = store
}

View file

@ -69,7 +69,7 @@ UserProfile := h.CachedPerKeyT(
### 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
package main
@ -92,7 +92,7 @@ var (
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(
5*time.Minute,
getUserContent,
h.WithStore(cache),
h.WithCacheStore(cache),
)
```

View file

@ -81,7 +81,7 @@ func ExampleCachedPerKeyT() {
}
// Example demonstrates using a memory-bounded LRU cache
func ExampleWithStore_lru() {
func ExampleWithCacheStore_lru() {
// Create an LRU cache that holds maximum 1000 items
lruStore := cache.NewLRUStore[any, string](1000)
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
@ -142,8 +142,6 @@ func (a *DistributedCacheAdapter) Set(key any, value string, ttl time.Duration)
a.cache.data[keyStr] = value
}
func (a *DistributedCacheAdapter) Delete(key any) {
a.cache.mutex.Lock()
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 {
a.cache.mutex.Lock()
defer a.cache.mutex.Unlock()
keyStr := fmt.Sprintf("htmgo:%v", key)
// Check if exists
if val, ok := a.cache.data[keyStr]; ok {
return val
}
// Compute and store
value := compute()
a.cache.data[keyStr] = value
// In a real implementation, you'd also set TTL in Redis
return value
}
@ -192,7 +190,7 @@ func ExampleDistributedCacheAdapter() {
// Use it with a cached component
SharedComponent := h.Cached(10*time.Minute, func() *h.Element {
return h.Div(h.Text("Shared across all servers"))
}, h.WithStore(adapter))
}, h.WithCacheStore(adapter))
html := h.Render(SharedComponent())
fmt.Printf("Cached in distributed store: %v\n", len(distCache.data) > 0)

View file

@ -46,7 +46,7 @@ func TestCached_WithCustomStore(t *testing.T) {
CachedDiv := Cached(1*time.Hour, func() *Element {
callCount++
return Div(Text(fmt.Sprintf("Rendered %d times", callCount)))
}, WithStore(lruStore))
}, WithCacheStore(lruStore))
// First render
html1 := Render(CachedDiv())
@ -115,7 +115,7 @@ func TestCachedPerKey_WithLRUStore(t *testing.T) {
renderCounts[userID]++
return Div(Text(fmt.Sprintf("User %d", userID)))
}
}, WithStore(lruStore))
}, WithCacheStore(lruStore))
// Render 2 users - fill cache to capacity
Render(UserProfile(1))
@ -214,7 +214,7 @@ func TestCachedPerKeyT_WithCustomStore(t *testing.T) {
P(Text(a.Content)),
)
}
}, WithStore(ttlStore))
}, WithCacheStore(ttlStore))
article1 := Article{ID: 1, Title: "First", Content: "Content 1"}
article2 := Article{ID: 2, Title: "Second", Content: "Content 2"}
@ -275,7 +275,7 @@ func TestCachedPerKey_ConcurrentAccess(t *testing.T) {
time.Sleep(10 * time.Millisecond)
return Div(Text(fmt.Sprintf("User %d", userID)))
}
}, WithStore(lruStore))
}, WithCacheStore(lruStore))
const numGoroutines = 50
const numUsers = 20
@ -419,7 +419,7 @@ func TestCachedNode_ClearCache(t *testing.T) {
CachedDiv := Cached(1*time.Hour, func() *Element {
callCount++
return Div(Text("Content"))
}, WithStore(lruStore))
}, WithCacheStore(lruStore))
// Render and cache
element := CachedDiv()

View file

@ -188,7 +188,7 @@ lruCache := cache.NewLRUStore[string, string](10000) // Max 10k items
var CachedUserProfile = h.CachedPerKeyT(
15*time.Minute,
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(
15*time.Minute,
getUserData,
h.WithStore(redisCache),
h.WithCacheStore(redisCache),
)
`
@ -312,7 +312,7 @@ ttlCache := cache.NewTTLStore[string, string]()
var CachedData = h.Cached(
5*time.Minute,
getData,
h.WithStore(ttlCache),
h.WithCacheStore(ttlCache),
)
`
@ -328,7 +328,7 @@ var CachedUserProfile = h.CachedPerKeyT(
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 {
return renderDashboard()
}, h.WithStore(memoryCache))
}, h.WithCacheStore(memoryCache))
var CachedUserData = h.CachedPerKeyT(15*time.Minute, func(userID string) (string, h.GetElementFunc) {
return userID, func() *h.Element {
return renderUserData(userID)
}
}, h.WithStore(memoryCache))
}, h.WithCacheStore(memoryCache))
`
const DistributedCacheExample = `
@ -395,7 +395,7 @@ var CachedSearchResults = h.CachedPerKeyT(
return performSearch(normalized)
}
},
h.WithStore(boundedCache),
h.WithCacheStore(boundedCache),
)
`