Coder Social home page Coder Social logo

go-simple-cache's Introduction

Go Simple Cache

Go Simple Cache is a lightweight, thread-safe, and easy-to-use caching library for Go. It provides a simple key-value store with expiration support, ideal for caching frequently accessed data to improve performance.

Features

  • Simple Interface: Straightforward API for setting, getting, deleting, and flushing items in the cache.
  • Expiration Support: Set expiration times for cached items to automatically remove stale data.
  • Thread-Safe: Utilizes mutex locks for safe concurrent access to the cache.
  • Lightweight: Minimal overhead and dependencies, making it suitable for a wide range of applications.
  • Easy Integration: Drop-in library that can be quickly integrated into existing projects.

Installation

You can install Go Simple Cache using go get:

go get github.com/nicolasbonnin/go-simple-cache

Usage

    package main

    import (
        "fmt"
        "time"
        "github.com/nicolasbonnin/go-simple-cache"
    )

    func main() {
        // Create a new instance of the cache
        cache := go_simple_cache.NewSimpleCache(1 * time.Hour)
	    
        // Set a key-value pair in the cache
        cache.Set("key", "value")
	    
        // Retrieve a value from the cache
        val, found := cache.Get("key")
        if found {
        	fmt.Println("Value found:", val)
        } else {
        	fmt.Println("Value not found")
        }
	
        // Delete an item from the cache
        cache.Delete("key")
	
        // Flush the entire cache
        cache.Flush()
    }

Use Case: Caching Database Results

Example of how to integrate the cache with database queries:

    // FindByNameWithCache fetches data by name from the cache if available,
    // otherwise, it fetches it from the database, caches it, and returns it.
    func (s *Service) FindByNameWithCache(name string) (*Data, error) {
        data, found := s.getDataFromCache(name)
        if !found {
            // Fetch data from the database
            data, err := s.FindByName(name)
            if err != nil {
                return nil, err
            }
            // Cache the fetched data
            s.simpleCache.Set(name, data)
            return data, nil
        }

        return data, nil
    }

    // CacheFlushByName removes the cached entry for the given name.
    func (s *Service) CacheFlushByName(name string) {
        s.simpleCache.Delete(name)
    }
	
    // getDataFromCache retrieves data from the cache by name.
    func (s *Service) getDataFromCache(name string) (*Data, bool) {
        cachedData, found := s.simpleCache.Get(name)
        if found {
            data, ok := cachedData.(*Data)
			
            return data, ok
        }
        return nil, false
    }

go-simple-cache's People

Contributors

nicolasbonnin avatar

Stargazers

 avatar

Watchers

 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.