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 (
	
	

	
	
)
Cache is a Redis-based cache.
type Cache struct {
	client *redis.Client
}
New creates a new Cache using the given Redis client.
func ( *redis.Client) *Cache {
	return &Cache{client: }
}
Get returns the value for key, or nil if the key does not exist.
func ( *Cache) ( context.Context,  string) ( []byte,  error) {
	defer derrors.Wrap(&, "Get(%q)", )
	,  := .client.Get(, ).Bytes()
	if  == redis.Nil { // not found
		return nil, nil
	}
	if  != nil {
		return nil, 
	}
	return , nil
}
Put inserts the key with the given data and time-to-live.
func ( *Cache) ( context.Context,  string,  []byte,  time.Duration) ( error) {
	defer derrors.Wrap(&, "Put(%q, data, %s)", , )
	_,  = .client.Set(, , , ).Result()
	return 
}
Clear deletes all entries from the cache.
func ( *Cache) ( context.Context) ( error) {
	defer derrors.Wrap(&, "Clear()")
	 := .client.FlushAll()
	return .Err()
}
Delete deletes the given keys. It does not return an error if a key does not exist.
func ( *Cache) ( context.Context,  ...string) ( error) {
	defer derrors.Wrap(&, "Delete(%q)", )
	 := .client.Unlink(, ...) // faster, asynchronous delete
	return .Err()
}
DeletePrefix deletes all keys beginning with prefix.
func ( *Cache) ( context.Context,  string) ( error) {
	defer derrors.Wrap(&, "DeletePrefix(%q)", )
	 := .client.Scan(, 0, +"*", int64(scanCount)).Iterator()
	var  []string
	for .Next() {
		 = append(, .Val())
		if len() > scanCount {
			if  := .Delete(, ...);  != nil {
				return 
			}
			 = [:0]
		}
	}
	if .Err() != nil {
		return .Err()
	}
	if len() > 0 {
		return .Delete(, ...)
	}
	return nil
}
The "count" argument to the Redis SCAN command, which is a hint for how much work to perform. Also used as the batch size for Delete calls in DeletePrefix. var for testing.