From 3542b3a9338a286a18f732f3691665305c525625 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 16 Sep 2018 14:56:03 +0800 Subject: [PATCH] context --- session.go | 4 +++- session_context.go | 32 ++++++++++++++++++++++++++++++++ session_get.go | 16 +++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 session_context.go diff --git a/session.go b/session.go index 3775eb01..966cd2ce 100644 --- a/session.go +++ b/session.go @@ -5,6 +5,7 @@ package xorm import ( + "context" "database/sql" "encoding/json" "errors" @@ -51,7 +52,8 @@ type Session struct { lastSQL string lastSQLArgs []interface{} - err error + err error + context context.Context } // Clone copy all the session's content and return a new session diff --git a/session_context.go b/session_context.go new file mode 100644 index 00000000..510c8b48 --- /dev/null +++ b/session_context.go @@ -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{}), + } +} diff --git a/session_get.go b/session_get.go index 69194a23..2808a2c4 100644 --- a/session_get.go +++ b/session_get.go @@ -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) {