Skip to main content
Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

samber/hot operatorsโ€‹

This page lists all operators available in the samber/hot sub-package of ro.

Installโ€‹

First, import the sub-package in your project:

go get -u github.com/samber/ro/plugins/samber/hot
  • Gets from cache or fetches if not present.

    import (
    hot "github.com/samber/go-hot"
    "github.com/samber/ro"
    rohot "github.com/samber/ro/plugins/samber/hot"
    )

    cache := hot.New[string, string]()
    cache.Set("key1", "value1")

    obs := ro.Pipe(
    ro.Just("key1", "key2"),
    rohot.GetOrFetch[string, string](cache),
    )

    sub := obs.Subscribe(ro.PrintObserver[lo.Tuple2[string, bool]]())
    defer sub.Unsubscribe()

    // Next: {Value: value1, Exists: true}
    // Next: {Value: <empty>, Exists: false}
    // Completed
    Prototype:
    func GetOrFetch[K comparable, V any](cache *hot.HotCache[K, V])
  • GetOrFetchOrErrorโ€‹

    Gets values from hot cache or returns error if not found.

    import (
    "fmt"
    "time"

    "github.com/samber/ro"
    rohot "github.com/samber/ro/plugins/samber/hot"
    "github.com/redis/go-redis/v9"
    )

    cache := hot.NewHotCache[string, string](hot.HotCacheOptions[string, string]{
    TTL: 5 * time.Minute,
    })

    cache.Set("key1", "value1")

    obs := ro.Pipe(
    ro.Just("key1", "key2", "key3"),
    rohot.GetOrFetchOrError[string, string](cache),
    )

    sub := obs.Subscribe(
    ro.NewObserver[string](
    func(value string) {
    fmt.Printf("Next: %s\n", value)
    },
    func(err error) {
    fmt.Printf("Error: %v\n", err)
    },
    func() {
    fmt.Println("Completed")
    },
    ),
    )
    defer sub.Unsubscribe()

    // Next: value1
    // Error: rohot.GetOrFetchOrError: not found
    // Error: rohot.GetOrFetchOrError: not found
    // Completed
    Prototype:
    func GetOrFetchOrError[K comparable, V any](cache *hot.HotCache[K, V])
  • GetOrFetchOrSkipโ€‹

    Gets values from hot cache and skips items that are not found.

    import (
    "time"

    "github.com/samber/ro"
    rohot "github.com/samber/ro/plugins/samber/hot"
    "github.com/redis/go-redis/v9"
    )

    cache := hot.NewHotCache[string, string](hot.HotCacheOptions[string, string]{
    TTL: 5 * time.Minute,
    })

    cache.Set("key1", "value1")
    cache.Set("key3", "value3")

    obs := ro.Pipe(
    ro.Just("key1", "key2", "key3", "key4"),
    rohot.GetOrFetchOrSkip[string, string](cache),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: value1
    // Next: value3
    // Completed

    Only items found in the cache are emitted, missing items are silently skipped.

    Prototype:
    func GetOrFetchOrSkip[K comparable, V any](cache *hot.HotCache[K, V])
  • GetOrFetchManyโ€‹

    Gets multiple values from hot cache by keys.

    import (
    "time"

    "github.com/samber/ro"
    rohot "github.com/samber/ro/plugins/samber/hot"
    "github.com/redis/go-redis/v9"
    )

    cache := hot.NewHotCache[string, string](hot.HotCacheOptions[string, string]{
    TTL: 5 * time.Minute,
    })

    cache.Set("key1", "value1")
    cache.Set("key2", "value2")
    cache.Set("key4", "value4")

    obs := ro.Pipe(
    ro.Just(
    []string{"key1", "key2", "key3"},
    []string{"key2", "key4", "key5"},
    ),
    rohot.GetOrFetchMany[string, string](cache),
    )

    sub := obs.Subscribe(ro.PrintObserver[map[string]string]())
    defer sub.Unsubscribe()

    // Next: map[key1:value1 key2:value2]
    // Next: map[key2:value2 key4:value4]
    // Completed

    Returns a map containing only the keys that were found in the cache.

    Similar:
    Prototype:
    func GetOrFetchMany[K comparable, V any](cache *hot.HotCache[K, V])