Code Monkey home page Code Monkey logo

pgmemo's Introduction

pgmemo

This package strives to be a way to memoize compilcated postgres queries in redis assuming one knows the output from postgresql will not change (frequently).

Configuration

Configuration of the pgmemo server is done through a configuration file; this file is expressed in the JSON format. Allowable keys in the JSON file are any expressed in the libpq Connection String Parameter keywords. Values must be expressed as strings.

There are three other directives that are required in the configuration: redishost, redisport, and either zmq_bind or zmq_connect. redisport is the only value not represented as a string in the json configuration; it is an integer. redishost/port are the host and port that the redis server is listening on.

If you specify zmq_bind, the server will bind the zmq socket to the specified string. This is useful when you desire the pgmemo server to be the "non-moving part" in your architecture. When specifying zmq_connect, the socket is connected to the specified port. This is useful for systems where you want to have multiple pgmemo workers connecting to a central router (perhaps through MDP, etc).

Sample configuration file:

{
  "host": "8.8.8.8",
  "user" : "my_db_role",
  "password" : "my_password",
  "port" : "1234",
  "redishost" : "127.0.0.1",
  "redisport" : 6379,
  "zmq_bind" : "tcp://*:5555"
}

Building

This uses a cmake based build system; from the source directory: mkdir build; cd build; cmake ..; make

Running

As easy as /path/to/pgmemo configuration_file.

Querying

pgmemo uses a protocol buffer to perform it's communication. The required fields are the query field and the dbname field. If you want to force a refresh of the cache (after sending the cached value), you may set the refresh field to true. Example (C++):

  PGMemoRequest input;
  PGMemoRequest output;
  input.set_dbname("postgres");
  input.set_query("select clock_timestamp();");
  //send the input PGMemoRequest here
  //recieve into the output PGMemoRequest here
  std::cout << output.get_result_json() << std::endl; //JSON string of output
  //force a refresh
  input.set_refresh(true);
  //send the input PGMemoRequest here
  //recieve into the output PGMemoRequest here
  std::cout << output.get_result_json() << std::endl; //should be same as last result
  input.set_refresh(false);
  //send the input PGMemoRequest here
  //recieve into the output PGMemoRequest here
  std::cout << output.get_result_json() << std::endl; // Different result; refresh has been forced

Output

If the query returns a single row, it is returned as a json string of a single document with col_name:value values. If there are multiple rows returned, the result is a json dictionary containing {rows: [{<row 1>}, {<row 2>},...]}.

Prerequisites

This program requires the hiredis C bindings for redis and libpqxx, a protocol buffer library, as well as the FastBSON-Cpp and EasyZMQ-Cpp library. The prerequisites on a Debian/Ubuntu system may be installed with

sudo apt-get install libpqxx-dev libhiredis-dev libprotobuf-dev

pgmemo's People

Contributors

neloe avatar

Watchers

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