Skip to content

Commit

Permalink
Merge core package back into the main repository and split into serva…
Browse files Browse the repository at this point in the history
…l 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
  • Loading branch information
lunny committed Feb 24, 2020
1 parent eabfb48 commit bf25a77
Show file tree
Hide file tree
Showing 87 changed files with 4,456 additions and 3,198 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ xorm.test
test.db.sql

.idea/

*coverage.out
8 changes: 5 additions & 3 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"xorm.io/xorm/caches"

"github.com/stretchr/testify/assert"
)

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

oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)

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

oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)

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

oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)

assert.NoError(t, testEngine.Sync2(new(MailBox3)))
Expand Down
99 changes: 99 additions & 0 deletions caches/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package caches

import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"strings"
"time"

"xorm.io/xorm/schemas"
)

const (
// CacheExpired is default cache expired time
CacheExpired = 60 * time.Minute
// CacheMaxMemory is not use now
CacheMaxMemory = 256
// CacheGcInterval represents interval time to clear all expired nodes
CacheGcInterval = 10 * time.Minute
// CacheGcMaxRemoved represents max nodes removed when gc
CacheGcMaxRemoved = 20
)

// list all the errors
var (
ErrCacheMiss = errors.New("xorm/cache: key not found")
ErrNotStored = errors.New("xorm/cache: not stored")
// ErrNotExist record does not exist error
ErrNotExist = errors.New("Record does not exist")
)

// CacheStore is a interface to store cache
type CacheStore interface {
// key is primary key or composite primary key
// value is struct's pointer
// key format : <tablename>-p-<pk1>-<pk2>...
Put(key string, value interface{}) error
Get(key string) (interface{}, error)
Del(key string) error
}

// Cacher is an interface to provide cache
// id format : u-<pk1>-<pk2>...
type Cacher interface {
GetIds(tableName, sql string) interface{}
GetBean(tableName string, id string) interface{}
PutIds(tableName, sql string, ids interface{})
PutBean(tableName string, id string, obj interface{})
DelIds(tableName, sql string)
DelBean(tableName string, id string)
ClearIds(tableName string)
ClearBeans(tableName string)
}

func encodeIds(ids []schemas.PK) (string, error) {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(ids)

return buf.String(), err
}

func decodeIds(s string) ([]schemas.PK, error) {
pks := make([]schemas.PK, 0)

dec := gob.NewDecoder(strings.NewReader(s))
err := dec.Decode(&pks)

return pks, err
}

// GetCacheSql returns cacher PKs via SQL
func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]schemas.PK, error) {
bytes := m.GetIds(tableName, GenSqlKey(sql, args))
if bytes == nil {
return nil, errors.New("Not Exist")
}
return decodeIds(bytes.(string))
}

// PutCacheSql puts cacher SQL and PKs
func PutCacheSql(m Cacher, ids []schemas.PK, tableName, sql string, args interface{}) error {
bytes, err := encodeIds(ids)
if err != nil {
return err
}
m.PutIds(tableName, GenSqlKey(sql, args), bytes)
return nil
}

// GenSqlKey generates cache key
func GenSqlKey(sql string, args interface{}) string {
return fmt.Sprintf("%v-%v", sql, args)
}
20 changes: 9 additions & 11 deletions cache_lru.go → caches/cache_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm
package caches

import (
"container/list"
"fmt"
"sync"
"time"

"xorm.io/core"
)

// LRUCacher implments cache object facilities
Expand All @@ -19,23 +17,23 @@ type LRUCacher struct {
sqlList *list.List
idIndex map[string]map[string]*list.Element
sqlIndex map[string]map[string]*list.Element
store core.CacheStore
store CacheStore
mutex sync.Mutex
MaxElementSize int
Expired time.Duration
GcInterval time.Duration
}

// NewLRUCacher creates a cacher
func NewLRUCacher(store core.CacheStore, maxElementSize int) *LRUCacher {
func NewLRUCacher(store CacheStore, maxElementSize int) *LRUCacher {
return NewLRUCacher2(store, 3600*time.Second, maxElementSize)
}

// NewLRUCacher2 creates a cache include different params
func NewLRUCacher2(store core.CacheStore, expired time.Duration, maxElementSize int) *LRUCacher {
func NewLRUCacher2(store CacheStore, expired time.Duration, maxElementSize int) *LRUCacher {
cacher := &LRUCacher{store: store, idList: list.New(),
sqlList: list.New(), Expired: expired,
GcInterval: core.CacheGcInterval, MaxElementSize: maxElementSize,
GcInterval: CacheGcInterval, MaxElementSize: maxElementSize,
sqlIndex: make(map[string]map[string]*list.Element),
idIndex: make(map[string]map[string]*list.Element),
}
Expand All @@ -57,7 +55,7 @@ func (m *LRUCacher) GC() {
defer m.mutex.Unlock()
var removedNum int
for e := m.idList.Front(); e != nil; {
if removedNum <= core.CacheGcMaxRemoved &&
if removedNum <= CacheGcMaxRemoved &&
time.Now().Sub(e.Value.(*idNode).lastVisit) > m.Expired {
removedNum++
next := e.Next()
Expand All @@ -71,7 +69,7 @@ func (m *LRUCacher) GC() {

removedNum = 0
for e := m.sqlList.Front(); e != nil; {
if removedNum <= core.CacheGcMaxRemoved &&
if removedNum <= CacheGcMaxRemoved &&
time.Now().Sub(e.Value.(*sqlNode).lastVisit) > m.Expired {
removedNum++
next := e.Next()
Expand Down Expand Up @@ -268,11 +266,11 @@ type sqlNode struct {
}

func genSQLKey(sql string, args interface{}) string {
return fmt.Sprintf("%v-%v", sql, args)
return fmt.Sprintf("%s-%v", sql, args)
}

func genID(prefix string, id string) string {
return fmt.Sprintf("%v-%v", prefix, id)
return fmt.Sprintf("%s-%s", prefix, id)
}

func newIDNode(tbName string, id string) *idNode {
Expand Down
6 changes: 3 additions & 3 deletions cache_lru_test.go → caches/cache_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm
package caches

import (
"testing"

"github.com/stretchr/testify/assert"
"xorm.io/core"
"xorm.io/xorm/schemas"
)

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

tableName := "cache_object1"
pks := []core.PK{
pks := []schemas.PK{
{1},
{2},
}
Expand Down
6 changes: 2 additions & 4 deletions cache_memory_store.go → caches/cache_memory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm
package caches

import (
"sync"

"xorm.io/core"
)

var _ core.CacheStore = NewMemoryStore()
var _ CacheStore = NewMemoryStore()

// MemoryStore represents in-memory store
type MemoryStore struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm
package caches

import (
"testing"
Expand Down
7 changes: 7 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,10 @@ func asBool(bs []byte) (bool, error) {
}
return strconv.ParseBool(string(bs))
}

// Conversion is an interface. A type implements Conversion will according
// the custom method to fill into database and retrieve from database.
type Conversion interface {
FromDB([]byte) error
ToDB() ([]byte, error)
}
Loading

0 comments on commit bf25a77

Please sign in to comment.