goredisstore

package
v2.2.2+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2018 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package goredisstore offers Redis-based store implementation for throttled using go-redis.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoRedisStore

type GoRedisStore struct {
	// contains filtered or unexported fields
}

GoRedisStore implements a Redis-based store using go-redis.

func New

func New(client *redis.Client, keyPrefix string) (*GoRedisStore, error)

New creates a new Redis-based store, using the provided pool to get its connections. The keys will have the specified keyPrefix, which may be an empty string, and the database index specified by db will be selected to store the keys. Any updating operations will reset the key TTL to the provided value rounded down to the nearest second. Depends on Redis 2.6+ for EVAL support.

Example

Demonstrates that how to initialize a RateLimiter with redis using go-redis library.

package main

import (
	"log"
	"time"

	"github.com/go-redis/redis"
	"github.com/throttled/throttled"
	"github.com/throttled/throttled/store/goredisstore"
)

func main() {
	// import "github.com/go-redis/redis"

	// Initialize a redis client using go-redis
	client := redis.NewClient(&redis.Options{
		PoolSize:    10, // default
		IdleTimeout: 30 * time.Second,
		Addr:        "localhost:6379",
		Password:    "", // no password set
		DB:          0,  // use default DB
	})

	// Setup store
	store, err := goredisstore.New(client, "throttled:")
	if err != nil {
		log.Fatal(err)
	}

	// Setup quota
	quota := throttled.RateQuota{MaxRate: throttled.PerMin(20), MaxBurst: 5}

	// Then, use store and quota as arguments for NewGCRARateLimiter()
	throttled.NewGCRARateLimiter(store, quota)
}
Output:

func (*GoRedisStore) CompareAndSwapWithTTL

func (r *GoRedisStore) CompareAndSwapWithTTL(key string, old, new int64, ttl time.Duration) (bool, error)

CompareAndSwapWithTTL atomically compares the value at key to the old value. If it matches, it sets it to the new value and returns true. Otherwise, it returns false. If the key does not exist in the store, it returns false with no error. If the swap succeeds, the ttl for the key is updated atomically.

func (*GoRedisStore) GetWithTime

func (r *GoRedisStore) GetWithTime(key string) (int64, time.Time, error)

GetWithTime returns the value of the key if it is in the store or -1 if it does not exist. It also returns the current time at the redis server to microsecond precision.

func (*GoRedisStore) SetIfNotExistsWithTTL

func (r *GoRedisStore) SetIfNotExistsWithTTL(key string, value int64, ttl time.Duration) (bool, error)

SetIfNotExistsWithTTL sets the value of key only if it is not already set in the store it returns whether a new value was set. If a new value was set, the ttl in the key is also set, though this operation is not performed atomically.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL