Sha256: dd9223446d6f47cdb94a8719d0a4545512479b362eb0b72196f8c70156a0e68a

Contents?: true

Size: 1.66 KB

Versions: 29

Compression:

Stored size: 1.66 KB

Contents

package betterlog

import (
	"context"
	"strings"

	"github.com/go-redis/redis"
	"golang.org/x/crypto/acme/autocert"
)

type RedisCertCache struct {
	Redis  *redis.Client
	PREFIX string
}

// Get reads certificate data from the specified key name.
func (cache RedisCertCache) Get(ctx context.Context, name string) ([]byte, error) {
	name = strings.Join([]string{cache.PREFIX, name}, "/")
	done := make(chan struct{})
	var (
		err  error
		data string
	)
	go func() {
		defer close(done)
		result := cache.Redis.Get(name)
		err = result.Err()
		if err == nil {
			data, err = result.Result()
		}
	}()
	select {
	case <-ctx.Done():
		return nil, ctx.Err()
	case <-done:
	}
	if err == redis.Nil {
		return nil, autocert.ErrCacheMiss
	}
	return []byte(data), err
}

// Put writes the certificate data to the specified redis key name.
func (cache RedisCertCache) Put(ctx context.Context, name string, data []byte) error {
	name = strings.Join([]string{cache.PREFIX, name}, "/")
	done := make(chan struct{})
	var err error
	go func() {
		defer close(done)
		select {
		case <-ctx.Done():
			// Don't overwrite the key if the context was canceled.
		default:
			result := cache.Redis.Set(name, string(data), 0)
			err = result.Err()
		}
	}()
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-done:
	}
	return err
}

// Delete removes the specified key name.
func (cache RedisCertCache) Delete(ctx context.Context, name string) error {
	name = strings.Join([]string{cache.PREFIX, name}, "/")
	var (
		err  error
		done = make(chan struct{})
	)
	go func() {
		defer close(done)
		err = cache.Redis.Del(name).Err()
	}()
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-done:
	}
	return err
}

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
betterlog-0.20.3 betterlog/redis_cert_cache.go
betterlog-0.20.2 betterlog/redis_cert_cache.go
betterlog-0.20.1 betterlog/redis_cert_cache.go
betterlog-0.20.0 betterlog/redis_cert_cache.go
betterlog-0.19.0 betterlog/redis_cert_cache.go
betterlog-0.18.0 betterlog/redis_cert_cache.go
betterlog-0.15.2 betterlog/redis_cert_cache.go
betterlog-0.15.1 betterlog/redis_cert_cache.go
betterlog-0.15.0 betterlog/redis_cert_cache.go
betterlog-0.14.1 betterlog/redis_cert_cache.go
betterlog-0.14.0 betterlog/redis_cert_cache.go
betterlog-0.13.1 betterlog/redis_cert_cache.go
betterlog-0.13.0 betterlog/redis_cert_cache.go
betterlog-0.12.2 betterlog/redis_cert_cache.go
betterlog-0.12.1 betterlog/redis_cert_cache.go
betterlog-0.12.0 betterlog/redis_cert_cache.go
betterlog-0.11.0 betterlog/redis_cert_cache.go
betterlog-0.10.0 betterlog/redis_cert_cache.go
betterlog-0.9.0 betterlog/redis_cert_cache.go
betterlog-0.8.1 betterlog/redis_cert_cache.go