context
This commit is contained in:
parent
fa23a9774a
commit
3542b3a933
|
@ -5,6 +5,7 @@
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -51,7 +52,8 @@ type Session struct {
|
||||||
lastSQL string
|
lastSQL string
|
||||||
lastSQLArgs []interface{}
|
lastSQLArgs []interface{}
|
||||||
|
|
||||||
err error
|
err error
|
||||||
|
context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone copy all the session's content and return a new session
|
// Clone copy all the session's content and return a new session
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2018 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 xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cacheContext struct {
|
||||||
|
context.Context
|
||||||
|
values map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cacheContext) Done() <-chan struct{} {
|
||||||
|
for k := range c.values {
|
||||||
|
delete(c.values, k)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cacheContext) Value(key interface{}) interface{} {
|
||||||
|
return c.values[key.(string)]
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCacher(ctx context.Context) context.Context {
|
||||||
|
return &cacheContext{
|
||||||
|
Context: ctx,
|
||||||
|
values: make(map[string]interface{}),
|
||||||
|
}
|
||||||
|
}
|
|
@ -66,7 +66,21 @@ func (session *Session) get(bean interface{}) (bool, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return session.nocacheGet(beanValue.Elem().Kind(), table, bean, sqlStr, args...)
|
if session.context != nil {
|
||||||
|
//res := session.context.Value(fmt.Sprintf("%v-%v", sql, args))
|
||||||
|
//runtime.deepcopy()
|
||||||
|
//&res
|
||||||
|
}
|
||||||
|
|
||||||
|
has, err := session.nocacheGet(beanValue.Elem().Kind(), table, bean, sqlStr, args...)
|
||||||
|
if err != nil || !has {
|
||||||
|
return has, err
|
||||||
|
}
|
||||||
|
if session.context != nil {
|
||||||
|
//session.context.
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) nocacheGet(beanKind reflect.Kind, table *core.Table, bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
|
func (session *Session) nocacheGet(beanKind reflect.Kind, table *core.Table, bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
|
||||||
|
|
Loading…
Reference in New Issue