Coder Social home page Coder Social logo

golangspell-redis's Introduction

golangspell-redis

Plugin with the Golang Spell commands for adding Redis capabilities to the project

Golang Spell

The Core project contains the core commands (and the respective templates) of the platform Golang Spell.

Test and coverage

Run the tests

TESTRUN=true go test ./... -coverprofile=cover.out

go tool cover -html=cover.out

Install golangci-lint and run lint:

golangci-lint run

Install

To install the golangspell-redis spell use the command

golangspell addspell github.com/golangspell/golangspell-redis golangspell-redis

Update

To update the golangspell-redis version use the command

golangspell updatespell github.com/golangspell/golangspell-redis golangspell-redis

Usage

Go to the project root and run the command:

golangspell redisinit

The command will add the Redis infrastructure files:

  • domain/cache.go: contains the Cache Interface with the cache component specification and the GetCache() function, which returns the cache component available in the application context

  • domain/lock.go: contains the Locker Interface containing the distributed Locker component specification, the Lock handle interface specification and the GetLocker() function, which returns the Locker component available in the application context

  • gateway/redis/redis_client.go: contains the Redis cache implementation

  • gateway/redis/redis_lock.go: contains the Redis lock implementation

For using the Locker feature follow this example:

	logger := config.GetLogger
	defer logger().Sync()

    lockIdentifier := "MyLockIdentifier"
    lock, err := domain.GetLocker().ObtainLock(lockIdentifier, 900*time.Millisecond)
    if err != nil {
		msg := fmt.Sprintf("Could not obtain lock for key %s. Message: %s\n", lockIdentifier, err.Error())
		logger().Error(msg)
		return err
	}

    //TODO: Add here the logic to be run protected from concurrent changes during your specified lock TTL

    _ = lock.Release()

For adding a value to the cache follow this example (in the example the TTL is 10 hours):

	logger := config.GetLogger
	defer logger().Sync()

    cacheKey := "MyCacheKey"
    cachedValue := "MyCachedValue"
    err := domain.GetCache().Set(cacheKey, cachedValue, 10*time.Hour)
    if err != nil {
		msg := fmt.Sprintf("Could not set a value in the cache. Message: %s\n", err.Error())
		logger().Error(msg)
		return err
	}

For getting a value from the cache follow this example:

	logger := config.GetLogger
	defer logger().Sync()

    cacheKey := "MyCacheKey"
    value, err := domain.GetCache().Get(cacheKey)
    if err != nil {
		msg := fmt.Sprintf("Could not set a value in the cache. Message: %s\n", err.Error())
		logger().Error(msg)
		return err
	}

    logger().Infof("My cache value is: %s", value)

Golang Spell logo

golangspell-redis's People

Contributors

danilovalente avatar danilovalententier avatar

Watchers

James Cloos 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.