gocache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2021 License: GPL-3.0 Imports: 5 Imported by: 0

README

Golang caching library

This library uses Redis or Memcached

Build

Usage example

package main

import (
    "fmt"
    "github.com/MrWebUzb/gocache"
)

func main() {
    // Generate default configurations
    // Default driver is a redis cache
    // For enabling memcached
    // use following code
    // cfg := gocache.NewConfig().WithDriver(gocache.MemcachedDriver)
    cfg := gocache.NewConfig()

    client, err := gocache.NewClient(cfg)

    if err != nil {
        fmt.Printf("could not connect to redis client: %v\n", err)
        return
    }

    if !client.Ping() {
        fmt.Printf("could not connect to redis client\n")
        return
    }

    if err := client.Set("test", "Test data"); err != nil {
        fmt.Printf("could not set data: %v\n", err)
        return
    }

    data := client.Get("test")

    fmt.Println(data)
}

Configurations

  1. WithDriver(gocache.Driver) // available driver types are RedisDriver, MemcachedDriver
  2. WithAddr(addr1, addr2) // replica addresses or single node address for both of redis and memcached
  3. WithCreds(username, password) // redis credentials
  4. WithDB(0) // database number in redis
  5. WithExpiration(10) // expiration in seconds

Documentation

Overview

1st of October, 2021 Author Asliddin Abdivasiyev Email space.coding.programmer@gmail.com This file provides configurations of library

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Ping() bool
	Get(key string) string
	Set(key string, data string) error
	Close() error
}

func NewClient

func NewClient(cfg *Config) (Client, error)

type Config

type Config struct {
	Driver     Driver   // driver type
	Addresses  []string // connection address
	Username   string   // username
	Password   string   // password
	DB         int      // database
	Expiration int      // expiration of the cache in seconds
}

Configuration of the package

func NewConfig

func NewConfig() *Config

Construct new configuration

func (*Config) WithAddr

func (c *Config) WithAddr(addresses ...string) *Config

Change connection addresses or set replica addresses

func (*Config) WithCreds

func (c *Config) WithCreds(username, password string) *Config

Change credentials

func (*Config) WithDB

func (c *Config) WithDB(db int) *Config

Change db

func (*Config) WithDriver

func (c *Config) WithDriver(driver Driver) *Config

Change default driver to your own

func (*Config) WithExpiration

func (c *Config) WithExpiration(exp int) *Config

Change expiration time

type Driver

type Driver int

Driver type Client selects redis or memcached

const (
	RedisDriver     Driver = iota // redis driver
	MemcachedDriver               // memcached driver
)

Available drivers

Jump to

Keyboard shortcuts

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