How to set cache expiration policy in Golang application?

王林
Release: 2024-06-04 12:05:57
Original
1100 people have browsed it

There are three ways to set cache expiration policy in Golang applications: use time.Duration: set a fixed expiration time. Use expiration timestamp: Explicitly specify the expiration time. Use a custom expiration policy: flexibly set the expiration time through redis.HookFunc.

如何在 Golang 应用中设置缓存过期策略?

How to set cache expiration policy in Golang application?

Using caching in Golang applications can significantly improve performance. However, there is a time limit for the existence of cached items, and they need to be invalidated after this limit is exceeded. Here's how to set cache expiration policy in Golang:

Using time.Duration

The easiest way is to use the time.Duration type, which represents a time span. For example:

import (
    "context"
    "time"

    "github.com/go-redis/redis/v8"
)

func main() {
    ctx := context.Background()
    client := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
    })

    // 设置缓存值,过期时间为 10 分钟
    err := client.SetEX(ctx, "my-key", "my-value", 10*time.Minute).Err()
    if err != nil {
        panic(err)
    }
}
Copy after login

Use expiration timestamp

Another approach is to use expiration timestamp, which is a Unix timestamp that indicates when the cache item expires. For example:

import (
    "context"
    "time"

    "github.com/go-redis/redis/v8"
)

func main() {
    ctx := context.Background()
    client := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
    })

    // 设置缓存值,到期时间戳为 10 分钟后的时间
    expiration := time.Now().Add(10 * time.Minute).Unix()
    err := client.Set(ctx, "my-key", "my-value", time.Duration(expiration-time.Now().Unix())*time.Second).Err()
    if err != nil {
        panic(err)
    }
}
Copy after login

Custom expiration strategy

If you need a more complex expiration strategy, you can use redis.HookFunc. For example, you can set a custom expiration time based on cache item usage:

import (
    "context"
    "time"

    "github.com/go-redis/redis/v8"
)

func main() {
    ctx := context.Background()
    client := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
    })

    // 设置自定义过期策略
    client.AddHook(redis.AfterSetHookFunc(func(ctx context.Context, key string, value interface{}) {
        // 根据缓存项的使用情况计算到期时间
        expiration := calculateExpiration(key, value)

        // 设置到期时间戳
        client.Expire(ctx, key, time.Duration(expiration-time.Now().Unix())*time.Second)
    }))
}
Copy after login

The above is the detailed content of How to set cache expiration policy in Golang application?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template