+7
-15
@@ -2,30 +2,22 @@ package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"honoka-chan/pkg/utils"
|
||||
"os"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *Instance
|
||||
Ldb *LdbInstance
|
||||
|
||||
ExampleDb = "assets/data.example.db"
|
||||
MainDb = "assets/main.db"
|
||||
UserDb = "assets/data.db"
|
||||
MainEng *xorm.Engine
|
||||
UserEng *xorm.Engine
|
||||
MainDb = "assets/main.db"
|
||||
UserDb = "assets/data.db"
|
||||
MainEng *xorm.Engine
|
||||
UserEng *xorm.Engine
|
||||
)
|
||||
|
||||
func init() {
|
||||
DB = GetInstance()
|
||||
|
||||
_, err := os.Stat(UserDb)
|
||||
if err != nil {
|
||||
utils.WriteAllText(UserDb, utils.ReadAllText(ExampleDb))
|
||||
}
|
||||
Ldb = GetLdbInstance()
|
||||
|
||||
eng, err := xorm.NewEngine("sqlite", MainDb)
|
||||
if err != nil {
|
||||
@@ -53,7 +45,7 @@ func init() {
|
||||
}
|
||||
|
||||
func MatchTokenUid(token, uid string) bool {
|
||||
res, err := DB.Get([]byte(uid))
|
||||
res, err := Ldb.Get([]byte(uid))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
|
||||
+6
-6
@@ -11,13 +11,13 @@ var (
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
type LdbInstance struct {
|
||||
db *leveldb.DB
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func GetInstance() *Instance {
|
||||
in := Instance{}
|
||||
func GetLdbInstance() *LdbInstance {
|
||||
in := LdbInstance{}
|
||||
once.Do(func() {
|
||||
var err error
|
||||
in.db, err = leveldb.OpenFile("./data/honoka-chan.db", nil)
|
||||
@@ -28,7 +28,7 @@ func GetInstance() *Instance {
|
||||
return &in
|
||||
}
|
||||
|
||||
func (in *Instance) Close() error {
|
||||
func (in *LdbInstance) Close() error {
|
||||
in.mu.Lock()
|
||||
defer in.mu.Unlock()
|
||||
if in.db != nil {
|
||||
@@ -37,7 +37,7 @@ func (in *Instance) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (in *Instance) Get(key []byte) ([]byte, error) {
|
||||
func (in *LdbInstance) Get(key []byte) ([]byte, error) {
|
||||
if len(key) == 0 {
|
||||
return nil, errors.New("empty key")
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (in *Instance) Get(key []byte) ([]byte, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (in *Instance) Set(key, value []byte) error {
|
||||
func (in *LdbInstance) Set(key, value []byte) error {
|
||||
if len(key) == 0 {
|
||||
return errors.New("empty key")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user