rediscache

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/boxgo/box/pkg/cache/rediscache"
	"github.com/boxgo/box/pkg/util/strutil"
)

var inst = rediscache.StdConfig("default").Build()

func main() {
	val := ""
	ctx := context.Background()
	testKey := strutil.RandomAlphabet(10)
	testVal := strutil.RandomAlphabet(100)

	if err := inst.Set(ctx, testKey, testVal, time.Second*5); err != nil {
		panic(err)
	}

	if err := inst.Get(ctx, testKey, &val); err != nil {
		panic(err)
	}

	fmt.Println(testVal == val)
}
Output:

true

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Default = StdConfig("default").Build()
)

Functions

func Get

func Get(ctx context.Context, key string, val interface{}) error
Example
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/boxgo/box/pkg/cache/rediscache"
	"github.com/boxgo/box/pkg/util/strutil"
)

var inst = rediscache.StdConfig("default").Build()

func main() {
	type (
		testStruct struct {
			String      string
			Int         int
			StringSlice []string
			Boolean     bool
			Map         map[string]interface{}
		}
	)

	ctx := context.Background()
	testKey := strutil.RandomAlphabet(10)
	testVal := testStruct{
		String:      "box",
		Int:         1234,
		StringSlice: []string{"tom", "andy", "luck"},
		Boolean:     true,
		Map: map[string]interface{}{
			"int":      99,
			"sliceInt": []int{1, 2, 3},
			"mapStringString": map[string]string{
				"key1": "val1",
				"key2": "val2",
			},
		},
	}

	err := inst.Set(ctx, testKey, testVal, time.Second*5)
	if err != nil {
		panic(err)
	}

	val := testStruct{}
	err = inst.Get(ctx, testKey, &val)
	a, _ := json.Marshal(val)
	b, _ := json.Marshal(testVal)

	fmt.Println(bytes.Compare(a, b) == 0)
}
Output:

true

func Set

func Set(ctx context.Context, key string, val interface{}, ttl time.Duration) error

Types

type Config

type Config struct {
	Prefix     string `config:"prefix" desc:"cache key prefix. if empty, auto prefix with format: ${serviceName}.cache.${key}."`
	Marshaller string `config:"marshaller" desc:"support json only"`
	Config     string `config:"config" desc:"redis config path. eg: 'default' means use 'redis.default' config"`
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig(key string) *Config

func StdConfig

func StdConfig(key string) *Config

func (*Config) Build

func (c *Config) Build() cache.Cache

func (*Config) Path

func (c *Config) Path() string

Jump to

Keyboard shortcuts

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