go-redis / cache

Cache library with Redis backend for Golang
https://redis.uptrace.dev/guide/go-redis-cache.html
BSD 2-Clause "Simplified" License
757 stars 95 forks source link

How to set item with no expiration? #89

Open ikrisliu opened 2 years ago

ikrisliu commented 2 years ago

When I set the item's TTL field to 0, the cache.ttl() function will reset TTL to default 1 hour.

croatiangrn commented 2 years ago

Set it to some negative number and that's it.

ikrisliu commented 2 years ago

I tried, but it's not working. Because it will be reset to 0 by the below codes.

func (item *Item) ttl() time.Duration {
    const defaultTTL = time.Hour

    if item.TTL < 0 {
        return 0
    }

    if item.TTL != 0 {
        if item.TTL < time.Second {
            log.Printf("too short TTL for key=%q: %s", item.Key, item.TTL)
            return defaultTTL
        }
        return item.TTL
    }

    return defaultTTL
}
jslang commented 1 year ago

Negative TTL behavior differs from what you'd expect: https://github.com/go-redis/cache/pull/84

Huskydog9988 commented 1 year ago

Based on my very short peruse of the codebase, I think only the check needs to be removed. (Assuming whatever issues #84 alludes too no longer exists.)