GithubHelp home page GithubHelp logo

andrewwdye / go-care Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tdv/go-care

0.0 0.0 0.0 25 KB

gRPC response memoization in Go is a codeless caching solution out of the box.

License: MIT License

Go 100.00%

go-care's Introduction

build workflow Go Reference

go-care

A library for gRPC response memoization in Go aims to improve common performance of any project having made real response computation lower cost.

go-care is an abbreviation of the 'Cached Response in Go'.

Caching might be almost a codeless solution.

Adding a couple gRPC interceptors to the project enables the caching usage at the both sides (client-, server-side) out of the box. Pay more attention to the logic and less on the secondary (caching at least).

A few examples will make the start easier.

Introduction

Having faced an issue of lack of the memoization in grpc this library has been written as a solution. Maybe there was a solution, but I had not been searching for the one enough well. Tricky question... Nonetheless, let's say a bit about the go-care package.

The package aims to improve performance via caching response for respective request params. Having included additional information from incoming data there is a possibility to make the most unique key for the cached response.

Essential gist of the approach is to make an interceptor wherein compute a key --a hash of the incoming data-- and memorize/restore a respective response, having been making the lower computation cost at the expense of the decreased number of the real computation.

The package can be used with built-in LRU cache or with an external cache like Redis, Memcached, or something else. In order to use an external cache there is only the need to implement a caching interface.

Features

  • server- and client-side response memoization
  • flexibility and room space for customization
  • loosely coupled architecture
  • thread safety
  • robust hash/key (any order of the same request data won't affect the hash/key)
  • easy to use :)

Note

  • ‘Robust key’ means that if a request contains a sequence --like array, slice, map-- the key will not be affected by the items' order within the sequence. All requests with the same data in spite of any order will be cached with the same response and there will be only one response computation for the first request.
  • Built-in in-memory cache implementation doesn't support an eviction policy by TTL. It has developed only for demo and small MVP. In production, you could use go-care with Redis, Memcached, or other cache. That might be done having implemented the 'Cache' interface and provided via 'Options'.

Usage

  1. Add the package to the project
  2. On the server-side it might be articulated like below (in pseudocode)
package main

import (
...
"github.com/tdv/go-care"
...
)

// Your server implementation
// type server struct {
//   api.UnimplementedYourServerServiceServer
// }

// ...

func main() {
	// Other your code
	...

	// Creating the options
	opts := care.NewOptions()

	// Adding methods for the memoization. 
	// You need to define the methods pool 
	// for response memoization.
	opts.Methods.Add("/api.GreeterService/SayHello", time.Second*60)

	// Other customization might be done via the opts variable.
	// See the examples.

	// Creating the server-side interceptor.
	unary := care.NewServerUnaryInterceptor(opts)
	// Providing / applying the interceptor
	grpcsrv := grpc.NewServer(unary)
	// Other your code

	...
}
  1. On the client-side the similar way
package main

import (
...
"github.com/tdv/go-care"
...
)

func main() {
   ...

   opts := care.NewDefaultOptions()
   opts.Methods.Add("/api.GreeterService/SayHello", time.Second*60)

   unary := care.NewClientUnaryInterceptor(opts)

   conn, err := grpc.Dial(
   	fmt.Sprintf("%s:%d", *host, *port),
   	grpc.WithTransportCredentials(insecure.NewCredentials()),
   	unary,
   )

   if err != nil {
   	log.Fatalf...
   }

   defer conn.Close()

   ...
}

More details you'll find among the examples

Examples

The examples demonstrate go-care package's features. Having run server and client with different param-set you can try out all features.

  • 'Greeter' is close to the canonical 'Hello World' example, demonstrating all basic features.
  • 'Redis Greeter' is the same, but with Redis like an external cache is.

Compiler and OS

The package has been developed and tested in Go 1.19 within Ubuntu 20.04. Hopefully, many other OS and compiler versions will be appropriate quite well.

go-care's People

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.