Skip to content

Commit bf25a77

Browse files
committed
Merge core package back into the main repository and split into serval sub packages. (#1543)
Fix test Improve fmt update go.mod Move core as a sub package Reviewed-on: https://gitea.com/xorm/xorm/pulls/1543
1 parent eabfb48 commit bf25a77

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+4456
-3198
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ xorm.test
3232
test.db.sql
3333

3434
.idea/
35+
36+
*coverage.out

cache_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"testing"
99
"time"
1010

11+
"xorm.io/xorm/caches"
12+
1113
"github.com/stretchr/testify/assert"
1214
)
1315

@@ -21,7 +23,7 @@ func TestCacheFind(t *testing.T) {
2123
}
2224

2325
oldCacher := testEngine.GetDefaultCacher()
24-
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
26+
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
2527
testEngine.SetDefaultCacher(cacher)
2628

2729
assert.NoError(t, testEngine.Sync2(new(MailBox)))
@@ -96,7 +98,7 @@ func TestCacheFind2(t *testing.T) {
9698
}
9799

98100
oldCacher := testEngine.GetDefaultCacher()
99-
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
101+
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
100102
testEngine.SetDefaultCacher(cacher)
101103

102104
assert.NoError(t, testEngine.Sync2(new(MailBox2)))
@@ -147,7 +149,7 @@ func TestCacheGet(t *testing.T) {
147149
}
148150

149151
oldCacher := testEngine.GetDefaultCacher()
150-
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
152+
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
151153
testEngine.SetDefaultCacher(cacher)
152154

153155
assert.NoError(t, testEngine.Sync2(new(MailBox3)))

caches/cache.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2019 The Xorm Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package caches
6+
7+
import (
8+
"bytes"
9+
"encoding/gob"
10+
"errors"
11+
"fmt"
12+
"strings"
13+
"time"
14+
15+
"xorm.io/xorm/schemas"
16+
)
17+
18+
const (
19+
// CacheExpired is default cache expired time
20+
CacheExpired = 60 * time.Minute
21+
// CacheMaxMemory is not use now
22+
CacheMaxMemory = 256
23+
// CacheGcInterval represents interval time to clear all expired nodes
24+
CacheGcInterval = 10 * time.Minute
25+
// CacheGcMaxRemoved represents max nodes removed when gc
26+
CacheGcMaxRemoved = 20
27+
)
28+
29+
// list all the errors
30+
var (
31+
ErrCacheMiss = errors.New("xorm/cache: key not found")
32+
ErrNotStored = errors.New("xorm/cache: not stored")
33+
// ErrNotExist record does not exist error
34+
ErrNotExist = errors.New("Record does not exist")
35+
)
36+
37+
// CacheStore is a interface to store cache
38+
type CacheStore interface {
39+
// key is primary key or composite primary key
40+
// value is struct's pointer
41+
// key format : <tablename>-p-<pk1>-<pk2>...
42+
Put(key string, value interface{}) error
43+
Get(key string) (interface{}, error)
44+
Del(key string) error
45+
}
46+
47+
// Cacher is an interface to provide cache
48+
// id format : u-<pk1>-<pk2>...
49+
type Cacher interface {
50+
GetIds(tableName, sql string) interface{}
51+
GetBean(tableName string, id string) interface{}
52+
PutIds(tableName, sql string, ids interface{})
53+
PutBean(tableName string, id string, obj interface{})
54+
DelIds(tableName, sql string)
55+
DelBean(tableName string, id string)
56+
ClearIds(tableName string)
57+
ClearBeans(tableName string)
58+
}
59+
60+
func encodeIds(ids []schemas.PK) (string, error) {
61+
buf := new(bytes.Buffer)
62+
enc := gob.NewEncoder(buf)
63+
err := enc.Encode(ids)
64+
65+
return buf.String(), err
66+
}
67+
68+
func decodeIds(s string) ([]schemas.PK, error) {
69+
pks := make([]schemas.PK, 0)
70+
71+
dec := gob.NewDecoder(strings.NewReader(s))
72+
err := dec.Decode(&pks)
73+
74+
return pks, err
75+
}
76+
77+
// GetCacheSql returns cacher PKs via SQL
78+
func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]schemas.PK, error) {
79+
bytes := m.GetIds(tableName, GenSqlKey(sql, args))
80+
if bytes == nil {
81+
return nil, errors.New("Not Exist")
82+
}
83+
return decodeIds(bytes.(string))
84+
}
85+
86+
// PutCacheSql puts cacher SQL and PKs
87+
func PutCacheSql(m Cacher, ids []schemas.PK, tableName, sql string, args interface{}) error {
88+
bytes, err := encodeIds(ids)
89+
if err != nil {
90+
return err
91+
}
92+
m.PutIds(tableName, GenSqlKey(sql, args), bytes)
93+
return nil
94+
}
95+
96+
// GenSqlKey generates cache key
97+
func GenSqlKey(sql string, args interface{}) string {
98+
return fmt.Sprintf("%v-%v", sql, args)
99+
}

cache_lru.go renamed to caches/cache_lru.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package xorm
5+
package caches
66

77
import (
88
"container/list"
99
"fmt"
1010
"sync"
1111
"time"
12-
13-
"xorm.io/core"
1412
)
1513

1614
// LRUCacher implments cache object facilities
@@ -19,23 +17,23 @@ type LRUCacher struct {
1917
sqlList *list.List
2018
idIndex map[string]map[string]*list.Element
2119
sqlIndex map[string]map[string]*list.Element
22-
store core.CacheStore
20+
store CacheStore
2321
mutex sync.Mutex
2422
MaxElementSize int
2523
Expired time.Duration
2624
GcInterval time.Duration
2725
}
2826

2927
// NewLRUCacher creates a cacher
30-
func NewLRUCacher(store core.CacheStore, maxElementSize int) *LRUCacher {
28+
func NewLRUCacher(store CacheStore, maxElementSize int) *LRUCacher {
3129
return NewLRUCacher2(store, 3600*time.Second, maxElementSize)
3230
}
3331

3432
// NewLRUCacher2 creates a cache include different params
35-
func NewLRUCacher2(store core.CacheStore, expired time.Duration, maxElementSize int) *LRUCacher {
33+
func NewLRUCacher2(store CacheStore, expired time.Duration, maxElementSize int) *LRUCacher {
3634
cacher := &LRUCacher{store: store, idList: list.New(),
3735
sqlList: list.New(), Expired: expired,
38-
GcInterval: core.CacheGcInterval, MaxElementSize: maxElementSize,
36+
GcInterval: CacheGcInterval, MaxElementSize: maxElementSize,
3937
sqlIndex: make(map[string]map[string]*list.Element),
4038
idIndex: make(map[string]map[string]*list.Element),
4139
}
@@ -57,7 +55,7 @@ func (m *LRUCacher) GC() {
5755
defer m.mutex.Unlock()
5856
var removedNum int
5957
for e := m.idList.Front(); e != nil; {
60-
if removedNum <= core.CacheGcMaxRemoved &&
58+
if removedNum <= CacheGcMaxRemoved &&
6159
time.Now().Sub(e.Value.(*idNode).lastVisit) > m.Expired {
6260
removedNum++
6361
next := e.Next()
@@ -71,7 +69,7 @@ func (m *LRUCacher) GC() {
7169

7270
removedNum = 0
7371
for e := m.sqlList.Front(); e != nil; {
74-
if removedNum <= core.CacheGcMaxRemoved &&
72+
if removedNum <= CacheGcMaxRemoved &&
7573
time.Now().Sub(e.Value.(*sqlNode).lastVisit) > m.Expired {
7674
removedNum++
7775
next := e.Next()
@@ -268,11 +266,11 @@ type sqlNode struct {
268266
}
269267

270268
func genSQLKey(sql string, args interface{}) string {
271-
return fmt.Sprintf("%v-%v", sql, args)
269+
return fmt.Sprintf("%s-%v", sql, args)
272270
}
273271

274272
func genID(prefix string, id string) string {
275-
return fmt.Sprintf("%v-%v", prefix, id)
273+
return fmt.Sprintf("%s-%s", prefix, id)
276274
}
277275

278276
func newIDNode(tbName string, id string) *idNode {

cache_lru_test.go renamed to caches/cache_lru_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package xorm
5+
package caches
66

77
import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11-
"xorm.io/core"
11+
"xorm.io/xorm/schemas"
1212
)
1313

1414
func TestLRUCache(t *testing.T) {
@@ -20,7 +20,7 @@ func TestLRUCache(t *testing.T) {
2020
cacher := NewLRUCacher(store, 10000)
2121

2222
tableName := "cache_object1"
23-
pks := []core.PK{
23+
pks := []schemas.PK{
2424
{1},
2525
{2},
2626
}

cache_memory_store.go renamed to caches/cache_memory_store.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package xorm
5+
package caches
66

77
import (
88
"sync"
9-
10-
"xorm.io/core"
119
)
1210

13-
var _ core.CacheStore = NewMemoryStore()
11+
var _ CacheStore = NewMemoryStore()
1412

1513
// MemoryStore represents in-memory store
1614
type MemoryStore struct {

cache_memory_store_test.go renamed to caches/cache_memory_store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package xorm
5+
package caches
66

77
import (
88
"testing"

convert.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,10 @@ func asBool(bs []byte) (bool, error) {
346346
}
347347
return strconv.ParseBool(string(bs))
348348
}
349+
350+
// Conversion is an interface. A type implements Conversion will according
351+
// the custom method to fill into database and retrieve from database.
352+
type Conversion interface {
353+
FromDB([]byte) error
354+
ToDB() ([]byte, error)
355+
}

0 commit comments

Comments
 (0)