blob: 3330f90b9251d532d99ea54f361c00b0ee873f7b [file]
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package cache implements a redis-based page cache
// for pkgsite.
package cache
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"golang.org/x/pkgsite/internal/derrors"
)
// Cache is a Redis-based cache.
type Cache struct {
client *redis.Client
}
// New creates a new Cache using the given Redis client.
func New(client *redis.Client) *Cache {
return &Cache{client: client}
}
// Get returns the value for key, or nil if the key does not exist.
func (c *Cache) Get(ctx context.Context, key string) (value []byte, err error) {
defer derrors.Wrap(&err, "Get(%q)", key)
val, err := c.client.Get(ctx, key).Bytes()
if err == redis.Nil { // not found
return nil, nil
}
if err != nil {
return nil, err
}
return val, nil
}
// Put inserts the key with the given data and time-to-live.
func (c *Cache) Put(ctx context.Context, key string, data []byte, ttl time.Duration) (err error) {
defer derrors.Wrap(&err, "Put(%q, data, %s)", key, ttl)
_, err = c.client.Set(ctx, key, data, ttl).Result()
return err
}
// Clear deletes all entries from the cache.
func (c *Cache) Clear(ctx context.Context) (err error) {
defer derrors.Wrap(&err, "Clear()")
status := c.client.FlushAll(ctx)
return status.Err()
}