Giter Club home page Giter Club logo

gocacheable's Introduction

GoCacheable

Build Status Coverage Status Go Report Card

Golang function calls result cache made easy.

Install

To use this utility on your project, get it by running the following command:

go get github.com/josemiguelmelo/gocacheable

Providers

Currently, the providers available are:

  1. BigCache (https://github.com/allegro/bigcache)

  2. Redis (https://redis.io/)

For more information about providers, click here

How to use

Using cacheable is quite simple:

1. Create Cacheable Manager

First of all, you must create a Cacheable Manager. This can be done by calling a single method:

managerIdentifier := "manager_id"
cacheableManager := gocacheable.NewCacheableManager(managerIdentifier)

2. Add new module to the manager

A module is a feature related group, with a cache provider associated. In order to use gocacheable it is required to have at least one module.

To add a new module, you only need to add the following line to the code:

storageProvider := &bcProvider.BigCacheProvider{}
err := cacheableManager.AddModule(moduleName, storageProvider)

3. Cache function result

After having the cacheable manager and a module, to cache a function return you just need to call it inside Cacheable method.

Suppose you want to cache the function: func example() string

var outValue string
cacheKey := "test_object"
timeToLive := 2 * time.Second
err := cacheableManager.Cacheable(
    moduleName, 
    cacheKey, 
    func() (interface{}, error) {
        return example(), nil
    },
    &outValue,
    timeToLive
)

When using the code above, if the call is already cached, it returns the cached value. Otherwise, it call the function, caches the result and returns the result into &outValue.

gocacheable's People

Contributors

josemiguelmelo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.