| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "infra/gae/libs/wrapper" | 8 "infra/gae/libs/wrapper" |
| 9 "infra/gae/libs/wrapper/gae/commonErrors" | 9 "infra/gae/libs/wrapper/gae/commonErrors" |
| 10 "infra/gae/libs/wrapper/unsafe" | 10 "infra/gae/libs/wrapper/unsafe" |
| 11 "infra/libs/clock" |
| 11 "sync" | 12 "sync" |
| 12 "time" | 13 "time" |
| 13 | 14 |
| 14 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
| 15 | 16 |
| 16 "appengine/memcache" | 17 "appengine/memcache" |
| 17 ) | 18 ) |
| 18 | 19 |
| 19 type memcacheData struct { | 20 type memcacheData struct { |
| 20 wrapper.BrokenFeatures | 21 wrapper.BrokenFeatures |
| 21 | 22 |
| 22 lock sync.Mutex | 23 lock sync.Mutex |
| 23 items map[string]*unsafe.Item | 24 items map[string]*unsafe.Item |
| 24 casID uint64 | 25 casID uint64 |
| 25 } | 26 } |
| 26 | 27 |
| 27 // memcacheImpl binds the current connection's memcache data to an | 28 // memcacheImpl binds the current connection's memcache data to an |
| 28 // implementation of {wrapper.Memcache, wrapper.Testable}. | 29 // implementation of {wrapper.Memcache, wrapper.Testable}. |
| 29 type memcacheImpl struct { | 30 type memcacheImpl struct { |
| 30 wrapper.Memcache | 31 wrapper.Memcache |
| 31 | 32 |
| 32 » data *memcacheData | 33 » data *memcacheData |
| 33 » timeNow func() time.Time | 34 » ctx context.Context |
| 34 } | 35 } |
| 35 | 36 |
| 36 var ( | 37 var ( |
| 37 _ = wrapper.Memcache((*memcacheImpl)(nil)) | 38 _ = wrapper.Memcache((*memcacheImpl)(nil)) |
| 38 _ = wrapper.Testable((*memcacheImpl)(nil)) | 39 _ = wrapper.Testable((*memcacheImpl)(nil)) |
| 39 ) | 40 ) |
| 40 | 41 |
| 41 // useMC adds a wrapper.Memcache implementation to context, accessible | 42 // useMC adds a wrapper.Memcache implementation to context, accessible |
| 42 // by wrapper.GetMC(c) | 43 // by wrapper.GetMC(c) |
| 43 func useMC(c context.Context) context.Context { | 44 func useMC(c context.Context) context.Context { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 54 mcd = &memcacheData{ | 55 mcd = &memcacheData{ |
| 55 BrokenFeatures: wrapper.BrokenFeatures{ | 56 BrokenFeatures: wrapper.BrokenFeatures{ |
| 56 DefaultError: commonErrors.ErrServerErro
rMC}, | 57 DefaultError: commonErrors.ErrServerErro
rMC}, |
| 57 items: map[string]*unsafe.Item{}} | 58 items: map[string]*unsafe.Item{}} |
| 58 mcdMap[ns] = mcd | 59 mcdMap[ns] = mcd |
| 59 } | 60 } |
| 60 | 61 |
| 61 return &memcacheImpl{ | 62 return &memcacheImpl{ |
| 62 wrapper.DummyMC(), | 63 wrapper.DummyMC(), |
| 63 mcd, | 64 mcd, |
| 64 » » » func() time.Time { return wrapper.GetTimeNow(ic) }, | 65 » » » ic, |
| 65 } | 66 } |
| 66 }) | 67 }) |
| 67 } | 68 } |
| 68 | 69 |
| 69 func (m *memcacheImpl) mkItemLocked(i *memcache.Item) *unsafe.Item { | 70 func (m *memcacheImpl) mkItemLocked(i *memcache.Item) *unsafe.Item { |
| 70 m.data.casID++ | 71 m.data.casID++ |
| 71 var exp time.Duration | 72 var exp time.Duration |
| 72 if i.Expiration != 0 { | 73 if i.Expiration != 0 { |
| 73 » » exp = time.Duration(m.timeNow().Add(i.Expiration).UnixNano()) | 74 » » exp = time.Duration(clock.Now(m.ctx).Add(i.Expiration).UnixNano(
)) |
| 74 } | 75 } |
| 75 newItem := unsafe.Item{ | 76 newItem := unsafe.Item{ |
| 76 Key: i.Key, | 77 Key: i.Key, |
| 77 Value: make([]byte, len(i.Value)), | 78 Value: make([]byte, len(i.Value)), |
| 78 Flags: i.Flags, | 79 Flags: i.Flags, |
| 79 Expiration: exp, | 80 Expiration: exp, |
| 80 CasID: m.data.casID, | 81 CasID: m.data.casID, |
| 81 } | 82 } |
| 82 copy(newItem.Value, i.Value) | 83 copy(newItem.Value, i.Value) |
| 83 return &newItem | 84 return &newItem |
| 84 } | 85 } |
| 85 | 86 |
| 86 func copyBack(i *unsafe.Item) *memcache.Item { | 87 func copyBack(i *unsafe.Item) *memcache.Item { |
| 87 ret := &memcache.Item{ | 88 ret := &memcache.Item{ |
| 88 Key: i.Key, | 89 Key: i.Key, |
| 89 Value: make([]byte, len(i.Value)), | 90 Value: make([]byte, len(i.Value)), |
| 90 Flags: i.Flags, | 91 Flags: i.Flags, |
| 91 } | 92 } |
| 92 copy(ret.Value, i.Value) | 93 copy(ret.Value, i.Value) |
| 93 unsafe.MCSetCasID(ret, i.CasID) | 94 unsafe.MCSetCasID(ret, i.CasID) |
| 94 | 95 |
| 95 return ret | 96 return ret |
| 96 } | 97 } |
| 97 | 98 |
| 98 func (m *memcacheImpl) retrieve(key string) (*unsafe.Item, bool) { | 99 func (m *memcacheImpl) retrieve(key string) (*unsafe.Item, bool) { |
| 99 ret, ok := m.data.items[key] | 100 ret, ok := m.data.items[key] |
| 100 » if ok && ret.Expiration != 0 && ret.Expiration < time.Duration(m.timeNow
().UnixNano()) { | 101 » if ok && ret.Expiration != 0 && ret.Expiration < time.Duration(clock.Now
(m.ctx).UnixNano()) { |
| 101 ret = nil | 102 ret = nil |
| 102 ok = false | 103 ok = false |
| 103 delete(m.data.items, key) | 104 delete(m.data.items, key) |
| 104 } | 105 } |
| 105 return ret, ok | 106 return ret, ok |
| 106 } | 107 } |
| 107 | 108 |
| 108 func (m *memcacheImpl) BreakFeatures(err error, features ...string) { | 109 func (m *memcacheImpl) BreakFeatures(err error, features ...string) { |
| 109 m.data.BreakFeatures(err, features...) | 110 m.data.BreakFeatures(err, features...) |
| 110 } | 111 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 187 |
| 187 m.data.lock.Lock() | 188 m.data.lock.Lock() |
| 188 defer m.data.lock.Unlock() | 189 defer m.data.lock.Unlock() |
| 189 | 190 |
| 190 if _, ok := m.retrieve(key); ok { | 191 if _, ok := m.retrieve(key); ok { |
| 191 delete(m.data.items, key) | 192 delete(m.data.items, key) |
| 192 return nil | 193 return nil |
| 193 } | 194 } |
| 194 return memcache.ErrCacheMiss | 195 return memcache.ErrCacheMiss |
| 195 } | 196 } |
| OLD | NEW |