Exemplo n.º 1
0
// Init initializes ledis session provider.
// configs: data_dir=./app.db,db=0
func (p *LedisProvider) Init(expire int64, configs string) error {
	p.expire = expire

	cfg, err := ini.Load([]byte(strings.Replace(configs, ",", "\n", -1)))
	if err != nil {
		return err
	}

	db := 0
	opt := new(config.Config)
	for k, v := range cfg.Section("").KeysHash() {
		switch k {
		case "data_dir":
			opt.DataDir = v
		case "db":
			db = com.StrTo(v).MustInt()
		default:
			return fmt.Errorf("session/ledis: unsupported option '%s'", k)
		}
	}

	l, err := ledis.Open(opt)
	if err != nil {
		return fmt.Errorf("session/ledis: error opening db: %v", err)
	}
	p.c, err = l.Select(db)
	return err
}
Exemplo n.º 2
0
Arquivo: ledis.go Projeto: kenno/cache
// StartAndGC starts GC routine based on config string settings.
// AdapterConfig: data_dir=./app.db,db=0
func (c *LedisCacher) StartAndGC(opts cache.Options) error {
	c.interval = opts.Interval

	cfg, err := ini.Load([]byte(strings.Replace(opts.AdapterConfig, ",", "\n", -1)))
	if err != nil {
		return err
	}

	db := 0
	opt := new(config.Config)
	for k, v := range cfg.Section("").KeysHash() {
		switch k {
		case "data_dir":
			opt.DataDir = v
		case "db":
			db = com.StrTo(v).MustInt()
		default:
			return fmt.Errorf("session/ledis: unsupported option '%s'", k)
		}
	}

	l, err := ledis.Open(opt)
	if err != nil {
		return fmt.Errorf("session/ledis: error opening db: %v", err)
	}
	c.c, err = l.Select(db)
	if err != nil {
		return err
	}

	go c.startGC()
	return nil
}
Exemplo n.º 3
0
func NewStore(DbPath string) (*Store, error) {
	if !dry.FileExists(DbPath) {
		e := os.MkdirAll(DbPath, 0777)
		if e != nil {
			return nil, e
		}
	}
	db, e := bolt.Open(filepath.Join(DbPath, "bolt"), 0600, nil)
	if e != nil {
		return nil, e
	}
	e = db.Update(func(tx *bolt.Tx) (e error) {
		buckets := []string{SITE_BUCKET, URI_BUCKET, USER_AGENT_BUCKET, USER_ID_BUCKET,
			SESSION_ID_BUCKET}
		for _, v := range buckets {
			if e != nil {
				continue
			}
			_, e = tx.CreateBucketIfNotExists([]byte(v))
		}
		return
	})
	if e != nil {
		return nil, e
	}

	ldb, e := leveldb.OpenFile(filepath.Join(DbPath, "level"), nil)
	if e != nil {
		return nil, e
	}

	ledisdb, e := ledis.Open(&config.Config{DataDir: filepath.Join(DbPath, "ledis")})
	if e != nil {
		return nil, e
	}
	ledisdbFirst, e := ledisdb.Select(0)
	if e != nil {
		return nil, e
	}

	e = os.MkdirAll(filepath.Join(DbPath, BINLOGS_DIR), 0777)
	if e != nil {
		return nil, e
	}
	bl, e := NewWriteBinLog(filepath.Join(DbPath, BINLOGS_DIR, "today"))
	if e != nil {
		return nil, e
	}

	s := new(Store)
	s.db = db
	s.ldb = ldb
	s.ledisdb = ledisdbFirst
	s.binlog = bl
	s.DbPath = DbPath

	e = s.ArchiveBinlogIfNeeded()

	return s, e
}
Exemplo n.º 4
0
Arquivo: app.go Projeto: Abioy/ledisdb
func NewApp(cfg *config.Config) (*App, error) {
	if len(cfg.DataDir) == 0 {
		println("use default datadir %s", config.DefaultDataDir)
		cfg.DataDir = config.DefaultDataDir
	}

	app := new(App)

	app.quit = make(chan struct{})

	app.closed = false

	app.cfg = cfg

	var err error

	if app.info, err = newInfo(app); err != nil {
		return nil, err
	}

	if app.listener, err = net.Listen(netType(cfg.Addr), cfg.Addr); err != nil {
		return nil, err
	}

	if len(cfg.HttpAddr) > 0 {
		if app.httpListener, err = net.Listen(netType(cfg.HttpAddr), cfg.HttpAddr); err != nil {
			return nil, err
		}
	}

	if len(cfg.AccessLog) > 0 {
		if path.Dir(cfg.AccessLog) == "." {
			app.access, err = newAcessLog(path.Join(cfg.DataDir, cfg.AccessLog))
		} else {
			app.access, err = newAcessLog(cfg.AccessLog)
		}

		if err != nil {
			return nil, err
		}
	}

	if app.ldb, err = ledis.Open(cfg); err != nil {
		return nil, err
	}

	app.m = newMaster(app)

	app.openScript()

	return app, nil
}
Exemplo n.º 5
0
func ClearAllTokens() (int64, error) {
	l, err := ledis.Open(cfg)
	defer l.Close()
	if err != nil {
		return 0, err
	}

	db, err := l.Select(0)
	if err != nil {
		return 0, err
	}

	return db.FlushAll()
}
Exemplo n.º 6
0
Arquivo: app.go Projeto: Gohan/ledisdb
func NewApp(cfg *Config) (*App, error) {
	if len(cfg.DataDir) == 0 {
		return nil, fmt.Errorf("must set data_dir first")
	}

	if len(cfg.DB.DataDir) == 0 {
		cfg.DB.DataDir = cfg.DataDir
	}

	app := new(App)

	app.quit = make(chan struct{})

	app.closed = false

	app.cfg = cfg

	var err error

	if strings.Contains(cfg.Addr, "/") {
		app.listener, err = net.Listen("unix", cfg.Addr)
	} else {
		app.listener, err = net.Listen("tcp", cfg.Addr)
	}

	if err != nil {
		return nil, err
	}

	if len(cfg.AccessLog) > 0 {
		if path.Dir(cfg.AccessLog) == "." {
			app.access, err = newAcessLog(path.Join(cfg.DataDir, cfg.AccessLog))
		} else {
			app.access, err = newAcessLog(cfg.AccessLog)
		}

		if err != nil {
			return nil, err
		}
	}

	if app.ldb, err = ledis.Open(&cfg.DB); err != nil {
		return nil, err
	}

	app.m = newMaster(app)

	return app, nil
}
Exemplo n.º 7
0
/* New(string,bool) initializes a cache object,
takes as input database name and a boolean parameter stating whether a compression is needed or not.
*/
func New(dataDir string) (*Cache, error) {
	cfg := config.NewConfigDefault()
	dataDir = fmt.Sprint(cfg.DataDir, "/", dataDir)
	cfg.DataDir = dataDir
	cfg.Replication.Compression = false
	ledis_connection, err := ledis.Open(cfg)
	if err != nil {
		panic("Resource temporary unavailable, error opening a Connection!!!")
	}
	db, err := ledis_connection.Select(0)
	return &Cache{
		cfg,
		ledis_connection,
		db,
	}, err
}
Exemplo n.º 8
0
func main() {
	flag.Parse()

	if len(*configPath) == 0 {
		println("need ledis config file")
		return
	}

	data, err := ioutil.ReadFile(*configPath)
	if err != nil {
		println(err.Error())
		return
	}

	if len(*dumpPath) == 0 {
		println("need dump file")
		return
	}

	var cfg ledis.Config
	if err = json.Unmarshal(data, &cfg); err != nil {
		println(err.Error())
		return
	}

	if len(cfg.DataDir) == 0 {
		println("must set data dir")
		return
	}

	ldb, err := ledis.Open(&cfg)
	if err != nil {
		println("ledis open error ", err.Error())
		return
	}

	err = loadDump(&cfg, ldb)
	ldb.Close()

	if err != nil {
		println(err.Error())
		return
	}

	println("Load OK")
}
Exemplo n.º 9
0
func NewApp(cfg *Config) (*App, error) {
	if len(cfg.DataDir) == 0 {
		return nil, fmt.Errorf("must set data_dir first")
	}

	app := new(App)

	app.quit = make(chan struct{})

	app.closed = false

	app.cfg = cfg

	var err error

	if app.listener, err = net.Listen(netType(cfg.Addr), cfg.Addr); err != nil {
		return nil, err
	}

	if len(cfg.HttpAddr) > 0 {
		if app.httpListener, err = net.Listen(netType(cfg.HttpAddr), cfg.HttpAddr); err != nil {
			return nil, err
		}
	}

	if len(cfg.AccessLog) > 0 {
		if path.Dir(cfg.AccessLog) == "." {
			app.access, err = newAcessLog(path.Join(cfg.DataDir, cfg.AccessLog))
		} else {
			app.access, err = newAcessLog(cfg.AccessLog)
		}

		if err != nil {
			return nil, err
		}
	}

	if app.ldb, err = ledis.Open(cfg.NewLedisConfig()); err != nil {
		return nil, err
	}

	app.m = newMaster(app)

	return app, nil
}
Exemplo n.º 10
0
func InitDb() {
	initLedisFunc := func() {
		cfg := new(config.Config)
		cfg.DataDir = beego.AppConfig.String("ledisdb::DataDir")
		var err error
		nowLedis, err = ledis.Open(cfg)
		if err != nil {
			println(err.Error())
			panic(err)
		}
	}

	ledisOnce.Do(initLedisFunc)

	db, _ := beego.AppConfig.Int("ledisdb::DB")

	LedisDB, _ = nowLedis.Select(db)
}
Exemplo n.º 11
0
/*
Create a backend that stores pushed events in ledisdb.
*/
func NewLedis(dirname string) (Backend, error) {
	lcfg := config.NewConfigDefault()
	lcfg.DataDir = dirname
	lcfg.Addr = ""
	lcfg.Databases = 1

	ledis, err := ledis.Open(lcfg)
	if err != nil {
		return nil, err
	}

	db, err := ledis.Select(0)
	if err != nil {
		return nil, err
	}

	return &ledisBackend{dirname, ledis, db, sync.Mutex{}, map[string]*ledisStreamObj{}}, nil
}
Exemplo n.º 12
0
func InitDb() error {
	opt := &config.Config{
		DataDir: path.Join(setting.DataDir, "vessel.db"),
	}

	l, err := ledis.Open(opt)
	if err != nil {
		return fmt.Errorf("open Ledis DB: %v", err)
	}

	db := 0
	LedisDB, err = l.Select(db)
	if err != nil {
		return fmt.Errorf("select Ledis DB '%d': %v", db, err)
	}

	return nil
}
Exemplo n.º 13
0
func main() {
	flag.Parse()

	if len(*configPath) == 0 {
		println("need ledis config file")
		return
	}

	cfg, err := config.NewConfigWithFile(*configPath)
	if err != nil {
		println(err.Error())
		return
	}

	if len(*dumpPath) == 0 {
		println("need dump file")
		return
	}

	if len(cfg.DataDir) == 0 {
		println("must set data dir")
		return
	}

	ldb, err := ledis.Open(cfg)
	if err != nil {
		println("ledis open error ", err.Error())
		return
	}

	err = loadDump(cfg, ldb)
	ldb.Close()

	if err != nil {
		println(err.Error())
		return
	}

	println("Load OK")
}
Exemplo n.º 14
0
// Open ledis store.
func Open(path string, db int, dropDatabase bool) (data.Store, error) {
	if dropDatabase {
		if _, err := os.Stat(path); err == nil {
			os.RemoveAll(path)
		}
	}

	cfg := config.NewConfigDefault()
	cfg.DataDir = path

	l, err := ledis.Open(cfg)
	if err != nil {
		return nil, debug.Err("open", err)
	}

	d, err := l.Select(db)
	if err != nil {
		return nil, debug.Err("select", err)
	}

	return redis.New(&store{l, d}), nil
}
Exemplo n.º 15
0
/* New(string,bool) initializes a cache object,
takes as input database name and a boolean parameter stating whether a compression is needed or not.
*/
func New(dbName string, Iscompression bool) *Cache {
	cfg := config.NewConfigDefault()
	cfg.DBName = dbName
	cfg.Replication.Compression = Iscompression
	ledis_connection, err := ledis.Open(cfg)
	if err != nil {
		panic("Resource temporary unavailable, problem in opening a Connection!!!")

	}
	db, err := ledis_connection.Select(0)

	if err != nil {
		panic("Problem selecting a Database Connection!!!")

	}

	return &Cache{
		cfg,
		ledis_connection,
		db,
	}

}
Exemplo n.º 16
0
// init ledis session
// savepath like ledis server saveDataPath,pool size
// e.g. 127.0.0.1:6379,100,astaxie
func (lp *LedisProvider) SessionInit(maxlifetime int64, savePath string) error {
	var err error
	lp.maxlifetime = maxlifetime
	configs := strings.Split(savePath, ",")
	if len(configs) == 1 {
		lp.savePath = configs[0]
	} else if len(configs) == 2 {
		lp.savePath = configs[0]
		lp.db, err = strconv.Atoi(configs[1])
		if err != nil {
			return err
		}
	}
	cfg := new(config.Config)
	cfg.DataDir = lp.savePath
	nowLedis, err := ledis.Open(cfg)
	c, err = nowLedis.Select(lp.db)
	if err != nil {
		println(err)
		return nil
	}
	return nil
}
Exemplo n.º 17
0
func NewApp(cfg *config.Config) (*App, error) {
	if len(cfg.DataDir) == 0 {
		println("use default datadir %s", config.DefaultDataDir)
		cfg.DataDir = config.DefaultDataDir
	}

	app := new(App)

	app.quit = make(chan struct{})

	app.closed = false

	app.cfg = cfg

	app.slaves = make(map[string]*client)
	app.slaveSyncAck = make(chan uint64)

	app.rcs = make(map[*respClient]struct{})

	app.migrateClients = make(map[string]*goredis.Client)
	app.newMigrateKeyLockers()

	var err error

	if app.info, err = newInfo(app); err != nil {
		return nil, err
	}

	if app.listener, err = net.Listen(netType(cfg.Addr), cfg.Addr); err != nil {
		return nil, err
	}

	if len(cfg.HttpAddr) > 0 {
		if app.httpListener, err = net.Listen(netType(cfg.HttpAddr), cfg.HttpAddr); err != nil {
			return nil, err
		}
	}

	if len(cfg.AccessLog) > 0 {
		if path.Dir(cfg.AccessLog) == "." {
			app.access, err = newAcessLog(path.Join(cfg.DataDir, cfg.AccessLog))
		} else {
			app.access, err = newAcessLog(cfg.AccessLog)
		}

		if err != nil {
			return nil, err
		}
	}

	if app.snap, err = newSnapshotStore(cfg); err != nil {
		return nil, err
	}

	if len(app.cfg.SlaveOf) > 0 {
		//slave must readonly
		app.cfg.Readonly = true
	}

	if app.ldb, err = ledis.Open(cfg); err != nil {
		return nil, err
	}

	app.m = newMaster(app)

	app.openScript()

	app.ldb.AddNewLogEventHandler(app.publishNewLog)

	return app, nil
}
Exemplo n.º 18
0
package auth

import (
	lediscfg "github.com/siddontang/ledisdb/config"
	"github.com/siddontang/ledisdb/ledis"
)

var cfg *lediscfg.Config = lediscfg.NewConfigDefault()
var l, _ = ledis.Open(cfg)
var db, _ = l.Select(0)

// BlacklistToken adds the token to a ledis database
func BlacklistToken(token []byte, expireAt int64) error {

	db.Set(token, token)
	db.ExpireAt(token, expireAt)

	return nil
}

// IsBlackListed checks if a token is in the blacklist list
func IsBlacklisted(token []byte) (bool, error) {
	res, err := db.Get(token)
	if err != nil {
		return false, err
	}

	if res == nil {
		return false, err
	}