llmongodb/dbs.go

87 lines
1.6 KiB
Go

// dbs
package main
import (
"github.com/garyburd/redigo/redis"
// "github.com/fzzy/radix/redis"
"gopkg.in/mgo.v2"
"log"
"os"
"time"
)
var (
dbs = Dbs{
MongoUri: "mongodb://127.0.0.1:27018",
RedisUri: "127.0.0.1:6379",
}
)
type Dbs struct {
MongoUri string
RedisUri string
rdb *redis.Pool //*redis.Client
mdb *mgo.Session
ll *mgo.Collection
// us *mgo.Collection
}
type MongoLogin struct {
User string `json:"user"`
Protocol string `json:"protocol"`
Ip string `json:"ip"`
Date time.Time `json:"date"`
}
type UserLogin struct {
User string `json:"user"`
Date time.Time `json:"date"`
Lock bool `json:"lock"`
}
type Index struct {
User string `json:"user"`
Date time.Time `json:"date"`
}
func (db *Dbs) poolRedis() {
dbs.rdb = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", db.RedisUri)
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
/*
func (db *Dbs) connectRedis() {
var err error
db.rdb, err = redis.Dial("tcp", db.RedisUri)
if err != nil {
log.Println("Redis connect Error: ", err.Error())
os.Exit(-1)
}
}
*/
func (db *Dbs) connectMongo() {
var err error
db.mdb, err = mgo.Dial(db.MongoUri)
if err != nil {
log.Println("Mongodb connect Error: ", err.Error())
os.Exit(-3)
}
db.ll = db.mdb.DB("dovecot").C("lastlogin")
// db.us = db.mdb.DB("dovecot").C("userlogin")
}