mantle

package module
v0.0.0-...-2fd7b23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2015 License: MIT Imports: 1 Imported by: 1

README

Mantle

Go wrapper for SQL and NoSQL dbs. Separate interface for SQL and NoSQL Datastores. Use Mantle's pooling to monitor all interactions to external strores. Idea is to monitir all external datastore interactions thorugh a common pool and strictly moniter that.

  1. Currently using miniminal vitess pool as a common connection pool.
  2. If this pool is unable to serve some complex fuctionality, datastores' pool would be used extending methods like get(), return_to_pool() and get_active().

####Get the package: go get github.com/goibibo/mantle

####Code: package main

    import (
            "fmt"
            "github.com/goibibo/mantle"
            "time"
    )

    func main(){
            //sample data
            keyValue := map[string]interface{}{"key1":"val1", "key2":"val2", "key3":"val3"}
            //extra params to be passed to connection
            options := map[string]string{"db":"1"}
            hostNPort := []string{"localhost:6379"}

            orm := mantle.Orm{Driver: "redis", HostAndPorts: hostNPort}

            //selecting a particular db
            orm := mantle.Orm{Driver: "redis", HostAndPorts: hostNPort, Options: options}

            //default "localhost:6379 is used when hostAndPort is not passed"
            //orm := mantle.Orm{Driver: "redis"}

            //this connects to redis at localhost:6379 by default
            //orm := mantle.Orm{}

            connection := orm.NewNoSQL()

            fmt.Println(connection.Set("key", "value2")) //output: true
            fmt.Println(connection.Get("key"))           //value2
            fmt.Println(connection.Delete("key"))        //1
            fmt.Println(connection.Get("key"))           //""

            fmt.Println(connection.MSet(keyValue))       //true
            fmt.Println(connection.MGet("key3", "key2")) //[val3 val2]

            connection.Expire("key", 1)
            time.Sleep(1 * time.Second)
            fmt.Println(connection.Get("key"))           //""

            /*Execute any redis command*/
            connection.Execute("LPUSH", "test", "a")
            connection.Execute("LPUSH", "test", "b")
            connection.Execute("LPUSH", "test", "c")
            values, _ := connection.Execute("LRANGE", "test", 0, -1)
            fmt.Println(values)                          //[[99] [98] [97]]

            connection.Setex("key", 1, "value")
            fmt.Println(connection.Get("key"))           //value
            time.Sleep(1 * time.Second)
            fmt.Println(connection.Get("key"))           //""

            //For MySQL
            //DB query
            query := "select * from flight_controllerdata"

            connections := []string{"root:@tcp(127.0.0.1:3306)/bm"}
            //Create mantle Driver with settings
            orm := mantle.Orm{Driver: "mysql", HostAndPorts: connections}

            // Create a new connection
            conn := orm.NewSQL()

            //Get query response and print
            response, _ := conn.Select(query)
            fmt.Println(response)
    }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mantle

type Mantle interface {
	Get(key string) (string, error)
	Set(key string, value interface{}) (bool, error)
	Delete(keys ...interface{}) (int, error)
	Setex(key string, duration int, value interface{}) (bool, error)
	MGet(keys ...interface{}) ([]string, error)
	MSet(keyValMap map[string]interface{}) (bool, error)
	Expire(key string, duration int) (bool, error)
	Execute(cmd string, args ...interface{}) (interface{}, error)

	//Set methods used by cassandra and redis
	Smembers(key string) ([]string, error)
	SAdd(key string, values ...interface{}) (bool, error)
	SRem(key string, value string) (bool, error)
	Sismember(key string, member string) (bool, error)
	Sismembers(key string, members []string) ([]bool, error)

	//stats methods
	StatsJSON() string
}

only strings are supported

type MantleSQL

type MantleSQL interface {
	Select(key string) ([]map[string]interface{}, error)
}

type Orm

type Orm struct {
	//redis|memcache|cassandra
	Driver string
	//arrays of ip:port,ip:port
	HostAndPorts []string
	//pool size
	Capacity int
	//any other options thats needed for creating a connection
	Options map[string]string
}

This struct is exported

func (*Orm) GetRedisConn

func (o *Orm) GetRedisConn() (*mantle.RedisConn, error)

override mantle and get a redis client

func (*Orm) NewNoSQL

func (o *Orm) NewNoSQL() Mantle

mantle wrapper for NoSQL DBs.

func (*Orm) NewSQL

func (o *Orm) NewSQL() MantleSQL

mantle wrapper for SQL DBs.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL