Giter VIP home page Giter VIP logo

basic-redis-leaderboard-demo-go's Introduction

Basic Redis Leaderboard Demo Golang

Show how the redis works with Golang.

How it works?

How the data is stored:

  • The AAPL's details - market cap of 2,6 triillions and USA origin - are stored in a hash like below:
    • E.g HSET "company:AAPL" symbol "AAPL" market_cap "2600000000000" country USA
  • The Ranks of AAPL of 2,6 trillions are stored in a ZSET.
    • E.g ZADD companyLeaderboard 2600000000000 company:AAPL

How the data is accessed:

  • Top 10 companies:
    • E.g ZREVRANGE companyLeaderboard 0 9 WITHSCORES
  • All companies:
    • E.g ZREVRANGE companyLeaderboard 0 -1 WITHSCORES
  • Bottom 10 companies:
    • E.g ZRANGE companyLeaderboard 0 9 WITHSCORES
  • Between rank 10 and 15:
    • E.g ZREVRANGE companyLeaderboard 9 14 WITHSCORES
  • Show ranks of AAPL, FB and TSLA:
    • E.g ZSCORE companyLeaderBoard company:AAPL company:FB company:TSLA
  • Adding market cap to companies:
    • E.g ZINCRBY companyLeaderBoard 1000000000 "company:FB"
  • Reducing market cap to companies:
    • E.g ZINCRBY companyLeaderBoard -1000000000 "company:FB"
  • Companies over a Trillion:
    • E.g ZCOUNT companyLeaderBoard 1000000000000 +inf
  • Companies between 500 billion and 1 trillion:
    • E.g ZCOUNT companyLeaderBoard 500000000000 1000000000000

Code Example: Get top 10 companies

func (c Controller) Top10() ([]*Company, error) {
    companies, err := c.r.ZRevRange(keyLeaderBoard, 0, 9)
    if err != nil {
        return nil, err
    }
    c.buildCompanies(companies)
    c.buildRanks(companies)
    return companies, nil
}

How to run it locally?

Copy .env.example to create .env. And provide the values for environment variables if needed

  • REDIS_HOST: Redis server host
  • REDIS_PORT: Redis server port
  • REDIS_PASSWORD: Password to the server

Run demo

go get
go run

Follow: http://localhost:5000

Try it out

Deploy to Heroku

Run on Google Cloud

(See notes: How to run on Google Cloud)

How to run on Google Cloud

1. Click "Run on Google Cloud"

Add the right values as per your infratsructure:

[ ? ] Value of REDIS_HOST environment variable (Redis server host) <Enter your Redis Host URL>
[ ? ] Value of REDIS_PORT environment variable (Redis server PORT) <Redis Port>
[ ? ] Value of REDIS_PASSWORD environment variable (Redis server password) <Redis Password>
[ ? ] Value of API_PUBLIC_PATH environment variable (Public path to frontend, example `/api/public`) /api/
[ ? ] Value of IMPORT_PATH environment variable (Path to seed.json file for import, example `/api/seed.json`) seed.json
[ ? ] Value of API_PORT environment variable (Api public port, example `5000`) 5000

Open up the link under "Manage this application at Cloud Console" to open up "Edit and Deploy New Revision”

2. Click “Variables and Secrets”

3. Verify the connector

3. Access the app

Hence, you should be able to access Rate Limiting app

basic-redis-leaderboard-demo-go's People

Contributors

ajeetraina avatar beqdev avatar denist-huma avatar dougtidwell avatar olegpustovit avatar

basic-redis-leaderboard-demo-go's Issues

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.