glib

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2020 License: BSD-3-Clause Imports: 15 Imported by: 0

README

glib

Install glib

#install glib
go get github.com/carltd/glib
dep ensure

Usage

start consul as config center

consul agent -ui -server -data-dir=/tmp

vi ~/glib-test.go

intput content:

package main

import (
	"time"

	"github.com/micro/go-log"

	"github.com/carltd/glib"
	_ "github.com/carltd/glib/cache/memcache"
	_ "github.com/carltd/glib/cache/redis"
	"gopkg.in/mgo.v2/bson"
)

type User struct {
	Id   uint   `xorm:"id"`
	Name string `xorm:"name"`
}

const (
	cacheKey    = "test-k"
	cacheValue  = "1"
	cacheExpire = time.Hour
)

func main() {
	var (
		user   User
		err    error
		exists bool
	)

	// init glib with config center(consul)
	if err = glib.Init(glib.WithServiceDomain("com.carltd.srv.demo")); err != nil {
		log.Fatal(err)
	}
	defer glib.Destroy()

	// cache usage
	log.Log(glib.Cache("rc").Put(cacheKey, cacheValue, cacheExpire))
	log.Log(glib.Cache("mc").Put(cacheKey, cacheValue, cacheExpire))

	log.Log(glib.Cache("rc").Get(cacheKey))
	log.Log(glib.Cache("mc").Get(cacheKey))

	// mysql usage
	exists, err = glib.DB("db1").Get(&user)
	if err != nil {
		log.Fatal(err)
	}
	if exists {
		log.Logf("%+v", user)
	}

	// mongodb usage
	var v interface{}
	s := glib.MgoShareClone("mgo")
	err = s.DB("test").C("col1").Find(bson.M{}).One(&v)
	s.Close()
	if err != nil {
		log.Fatal(err)
	}
	log.Logf("%+v", v)


	s = glib.MgoShareCopy("mgo")
	err = s.DB("test").C("col1").Find(bson.M{}).One(&v)
	s.Close()
	if err != nil {
		log.Fatal(err)
	}
	log.Logf("%+v", v)
}

config consul kv \com.carltd.srv.demo\glib-supports:

{
    "db": true,
    "cache": true,
    "mgo": true
}

\com.carltd.srv.demo\glib-db:

[{
    "alias": "db1",
    "driver": "mysql",
    "showSql": false,
    "logLevel": "debug",
    "dsn": "root:@tcp(127.0.0.1:3306)/test",
    "enable": true,
    "ttl": 30
}]

\com.carltd.srv.demo\glib-cache:

[{
    "alias": "rc",
    "driver": "redis",
    "dsn": ":123456@127.0.0.1:6379/0",
    "enable": true,
    "ttl": 30
},{
    "alias":"mc",
    "driver":"memcache",
    "dsn": "127.0.0.1:11211",
    "enable": true,
    "ttl": 30
}]

\com.carltd.srv.demo\glib-mgo:

[{
    "enable": true,
    "alias": "mgo",
    "ttl":60,
    "dsn": "mongodb://127.0.0.1:27017"
}]

at last, run glib-test.go

go run glib-test.go

Documentation

Index

Constants

View Source
const (
	BrokerTypePublisher = "publisher"
	BrokerTypeConsumer  = "consumer"
)

Variables

This section is empty.

Functions

func Cache

func Cache(alias string) internal.Cacher

func Consumer

func Consumer(alias string) queue.Consumer

func DB

func DB(alias string) *gorm.DB

DB will return a instance of `xorm.EngineGroup`, panic if it's not exists

func Destroy

func Destroy() error

Destroy - 释放glib管理资源

func Init

func Init(opts ...option) error

Init enabled features

func MgoShareClone

func MgoShareClone(alias string) *mgo.Session

MgoShareClone will return a clone instance of `*mgo.Session`, panic if it's not exists example:

var v = make([]interface{}, 0)
s := glib.MgoShareClone("something")
defer s.Close()
// use it
err := s.DB("somedb").C("col").Find(&v)

func MgoShareCopy

func MgoShareCopy(alias string) *mgo.Session

MgoShareCopy will return a copy instance of `*mgo.Session`, panic if it's not exists example:

var v = make([]interface{}, 0)
s := glib.MgoShareCopy("something")
defer s.Close()
// use it
err := s.DB("somedb").C("col").Find(&v)

func NewId

func NewId() string

NewId - return a global unique id

func Publisher

func Publisher(alias string) queue.Publisher

func Redis added in v1.2.0

func Redis(alias string) redis_wrapper.RedisWrapper

func WithDiscoverAddr

func WithDiscoverAddr(addr string) option

WithDiscoverAddr - config center address

func WithNoStorage

func WithNoStorage() option

WithNoStorage - none db, cache, mgo etc.

func WithRunAt added in v1.4.2

func WithRunAt(addr string) option

WithRunAt - set the server's worker address

func WithServiceDomain

func WithServiceDomain(domain string) option

WithServiceDomain - set the service's domain used prefix

Types

type ConfigObject

type ConfigObject interface {
	StringSlice(def []string) []string
	StringMap(def map[string]string) map[string]string
	Scan(val interface{}) error
	Bytes() []byte
}

func GetConfig

func GetConfig(keyPath string) ConfigObject

get custom config Object

Directories

Path Synopsis
cache
message
Package message is a generated protocol buffer package.
Package message is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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