-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
52 lines (40 loc) · 1001 Bytes
/
kv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package kv
import (
"database/sql"
"errors"
"time"
"github.com/rubiojr/kv/driver/mysql"
"github.com/rubiojr/kv/driver/sqlite"
"github.com/rubiojr/kv/types"
)
const TABLE_NAME = "key_values"
const MAX_KEY_LENGTH = 255
const MAX_VALUE_LENGTH = 65535
type KV struct{}
type Database interface {
Init(tableName string, urn string) error
Get(key string) ([]byte, error)
MGet(...string) ([][]byte, error)
Set(key string, value []byte, expireAt *time.Time) error
MSet(kvs types.KeyValues, expireAt *time.Time) error
Del(key string) error
MDel(keys ...string) error
Exists(key string) (bool, error)
MExists(keys ...string) ([]bool, error)
Raw() *sql.DB
}
func New(driver string, urn string) (Database, error) {
var db Database
var err error
switch driver {
case "sqlite":
db = &sqlite.Database{}
err = db.Init(TABLE_NAME, urn)
case "mysql":
db = &mysql.Database{}
err = db.Init(TABLE_NAME, urn)
default:
err = errors.New("driver not supported")
}
return db, err
}