Git Product home page Git Product logo

in-memory-storage's Introduction

In-Memory Storage

This is a simple in-memory storage service that allows clients to add, delete, and retrieve key-value pairs via a REST API. The service also includes rate limiting functionality to limit the number of requests per second from a single IP address.

API

The API includes the following endpoints:

  • POST /set: Add a new key-value pair to the storage. The request body should include a JSON object with the key and value fields. An optional expiration field can be included to set a time-to-live value for the key in seconds.
  • DELETE /delete?key=: Delete the key-value pair with the specified key from the storage.
  • GET /get?key=: Retrieve the value for the key with the specified key from the storage.
  • GET /all: Retrieve all key-value pairs from the storage.

Object should be in the following format:

{
    "key": "key",
    "value": "value",
    "expiration": 60
}

as in this Entity struct:

// Entity represents a key-value pair in the in-memory storage.
type Entity struct {
	Key   string `json:"key"`
	Value string `json:"value"`
	// Expiration is the time in nanoseconds when the key-value pair will expire.
	Expiration int64 `json:"expiration"`
}

If a client exceeds the rate limit, the service will return a 429 Too Many Requests HTTP status code.

Configuration

The service can be configured using the environment variables listed below:
SERVER_PORT server port, default 8080
RATE_LIMIT rate limit in requests per second, default 10
This value is used to calculate how many nanoseconds to wait between requests from a single IP address.

Building and Running

To build the service, run the following command:

go build -o in-memory-storage cmd/in-memory-storage/main.go

To run the service, use the following command:

./in-memory-storage

The service can also be run using Docker. To build the Docker image, use the following command:

docker build -t in-memory-storage .

To run the Docker container, use the following command:

docker run -p 8080:8080 in-memory-storage

Testing

To run the tests for the service, use the following command:

go test ./...

in-memory-storage's People

Contributors

gynshu 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.