🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

weavelab.xyz/cachin

Package Overview
Dependencies
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weavelab.xyz/cachin

Go Modules
Version
v0.0.0-20240103221106-c7edb62195e6
Version published
Created
Source

Cachin

Cachin is a package for functional caching and memoization in Go.

To fetch, build and install from the GitHub source:

go get github.com/weave-lab/cachin

cache

The cache package provides functionality for caching function results. You can use these functions to cache function results both in memory and in an external data store. Be aware that if you are caching large amounts of data with long TTLs you may run into OOM issues.

It's important to note that the cached function may return expired data. This can happen when your cached function returns an error but the previous cache value still exists. In this case valid cache data will be returned along with your function's error. As the developer it is up to you to determine if this stale data is safe to use or if it should be ignored.

Example 1: cache function results in memory. This makes repeatedly calling the GetTeams function much faster since only the first call will result in a network call.

// GetTeams gets a list of teams from an external api. The results will be cached in memory for 
// at least one hour
var GetTeams = cache.InMemory(time.Hour, func(ctx context.Context) ([]Team, error) {
    client := &http.Client{}
    resp, err := client.Get("https://api.weavedev.net/teams")
    if err != nil {
        return nil, err
    }

    defer resp.Body.Close()
    body, err := io.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }

    var teams []Team
    err = json.Unmarshal(body, &teams)
    if err != nil {
        return nil, err
    }

    return teams, nil
})

Example 2: cache function results in memory and on disk. Like example 1 this improves performance. It also allows the cache to be restored across runs which can be useful for short-lived process like cron jobs or cli tools

// GetTeams gets a list of teams from an external api. The results will be cached in memory for at least one hour.
// Additionally, the cache will be backed by the file system so it can be restored between program runs
var GetTeams = cache.OnDisk(filepath.Join("cache", "teams"), time.Hour, func(ctx context.Context) ([]Team, error) {
    client := &http.Client{}
    resp, err := client.Get("https://api.weavedev.net/teams")
    if err != nil {
        return nil, err
    }

    defer resp.Body.Close()
    body, err := io.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }

    var teams []Team
    err = json.Unmarshal(body, &teams)
    if err != nil {
        return nil, err
    }

    return teams, nil
})

FAQs

Package last updated on 03 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts