llmongodb/dbs.go

84 lines
1.4 KiB
Go
Raw Normal View History

2015-07-30 15:55:32 +02:00
// dbs
package main
import (
"github.com/garyburd/redigo/redis"
// "github.com/fzzy/radix/redis"
"fmt"
2015-07-30 15:55:32 +02:00
"log"
"os"
"time"
"gopkg.in/mgo.v2"
2015-07-30 15:55:32 +02:00
)
2015-07-30 16:53:24 +02:00
var (
dbs = Dbs{
MongoUri: "mongodb://127.0.0.1:27018",
RedisUri: "127.0.0.1:6379",
}
)
2015-07-30 15:55:32 +02:00
type Dbs struct {
MongoUri string
RedisUri string
rdb *redis.Pool //*redis.Client
2015-07-30 15:55:32 +02:00
mdb *mgo.Session
ll *mgo.Collection
2015-10-16 11:43:18 +02:00
// us *mgo.Collection
2015-07-30 15:55:32 +02:00
}
type MongoLogin struct {
User string `json:"user"`
Protocol string `json:"protocol"`
Ip string `json:"ip"`
Date time.Time `json:"date"`
2015-10-16 11:43:18 +02:00
}
type Ips struct {
Ip string `json:"ip"`
}
2015-10-16 11:43:18 +02:00
type UserLogin struct {
User string `json:"user"`
Date time.Time `json:"date"`
Lock bool `json:"lock"`
2015-07-30 15:55:32 +02:00
}
type Index struct {
User string `json:"user"`
Date time.Time `json:"date"`
}
func (db *Dbs) poolRedis() {
dbs.rdb = &redis.Pool{
2015-11-19 17:12:53 +01:00
MaxIdle: 128,
MaxActive: 1000,
Wait: true,
IdleTimeout: 1 * 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
},
}
}
2015-07-30 15:55:32 +02:00
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("lastlogin").C(fmt.Sprintf("lastlogin_%s", opts.Month))
2015-07-30 15:55:32 +02:00
}