cache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 4 Imported by: 2

README

Mix Arch Cache Client

This is a client for the cache server. It is used to store and retrieve data from the cache server.

Documentation

Go Reference

Installation

You can install the package using the following command:

go get github.com/mixarchitecture/cache

Usage

To use the cache package, import it in your Go code:

import "github.com/mixarchitecture/cache"

Full Example

package main

import (
 "context"
 "fmt"

 "github.com/mixarchitecture/cache"
 "github.com/mixarchitecture/i18np"
 "github.com/mixarchitecture/mredis"
)

type Entity struct {
 ID string `json:"id"`
}

func main() {
 redis := mredis.New(&mredis.Config{
  Host: "localhost",
  Port: "6379",
  DB:   0,
 })
 c := cache.New[*Entity](redis)

 key := "my-cache-key"
 cacheHandler := func() (*Entity, *i18np.Error) {
  return &Entity{ID: "my-id"}, nil
 }
 res, err := c.Creator(createEntity).Handler(cacheHandler).Get(context.Background(), key)
 if err != nil {
  fmt.Println(err)
  // handle error
 }
 fmt.Println(res.ID)
}

func createEntity() *Entity {
 return &Entity{}
}

Error Keys

this package uses i18np to show error messages. Click here to learn how to use i18np.

Here is the list of error messages to be forwarded to i18np:

Key Description
cache_an_error_on_exist An error occurred while checking the existence of the cache
cache_an_error_on_get An error occurred while getting the cache
cache_an_error_on_set An error occurred while setting the cache
cache_not_runnable The cache is not runnable

The service cache uses in the background is flexible and you can add your own service if you want. However, we recommend mredis using redis.

Custom Service

If you want to write your own service, make sure your service implements the following interface!

// Service is an interface that defines the methods of a cache service
// It's implemented by the cache service
type Service interface {
 // Get returns a value and an error.
 Get(ctx context.Context, k string) (string, error)

 // Set sets a value and returns an error.
 SetEx(ctx context.Context, k string, v interface{}, d time.Duration) error

 // Set sets a value and returns an error.
 Exist(ctx context.Context, k string) (bool, error)
}

Contributing

Contributions are welcome! If you find a bug or want to add a new feature, please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client[Entity any] interface {
	// Get returns an entity and an error.
	Get(context.Context, string) (Entity, *i18np.Error)

	// Set sets an entity and returns an error.
	Handler(Handler[Entity]) Client[Entity]

	// Set sets an entity and returns an error.
	Creator(creator Creator[Entity]) Client[Entity]

	// Set sets an entity and returns an error.
	Timeout(time.Duration) Client[Entity]
}

Client is an interface that defines the methods of a cache client

func New

func New[Entity any](service Service) Client[Entity]

New returns a new cache client It receives a service that implements the Service interface and returns a Client interface

type Creator

type Creator[Entity any] func() Entity // Creator is a function that returns an entity. It's running when the entity is found in the cache and make a pointer to it

type Handler

type Handler[Entity any] func() (Entity, *i18np.Error) // Handler is a function that returns an entity and an error. It's running when the entity is not found in the cache

type Service

type Service interface {
	// Get returns a value and an error.
	Get(ctx context.Context, k string) (string, error)

	// Set sets a value and returns an error.
	SetEx(ctx context.Context, k string, v interface{}, d time.Duration) error

	// Set sets a value and returns an error.
	Exist(ctx context.Context, k string) (bool, error)
}

Service is an interface that defines the methods of a cache service It's implemented by the cache service

Jump to

Keyboard shortcuts

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