OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package dscache |
| 6 |
| 7 import ( |
| 8 "sync" |
| 9 |
| 10 "github.com/luci/gae/service/datastore" |
| 11 "github.com/luci/gae/service/memcache" |
| 12 "github.com/luci/luci-go/common/logging" |
| 13 ) |
| 14 |
| 15 type dsTxnState struct { |
| 16 sync.Mutex |
| 17 |
| 18 toLock []memcache.Item |
| 19 toDelete []string |
| 20 } |
| 21 |
| 22 // Reset sets the transaction state back to its 0 state. This is used so that |
| 23 // when a transaction retries the function, we don't accidentally leak state |
| 24 // from one function to the next. |
| 25 func (s *dsTxnState) Reset() { |
| 26 s.Lock() |
| 27 defer s.Unlock() |
| 28 // reduce capacity back to 0, but keep the allocated array. If the trans
action |
| 29 // body retries, it'll probably end up re-allocating the same amount of
space |
| 30 // anyway. |
| 31 s.toLock = s.toLock[:0] |
| 32 s.toDelete = s.toDelete[:0] |
| 33 } |
| 34 |
| 35 func (s *dsTxnState) Apply(mc memcache.Interface, log logging.Logger) { |
| 36 s.Lock() |
| 37 defer s.Unlock() |
| 38 |
| 39 err := mc.SetMulti(s.toLock) |
| 40 if err != nil { |
| 41 log.Errorf("dscache: txn.Apply SetMulti: %s", err) |
| 42 } |
| 43 } |
| 44 |
| 45 func (s *dsTxnState) Release(mc memcache.Interface, log logging.Logger) { |
| 46 s.Lock() |
| 47 defer s.Unlock() |
| 48 |
| 49 err := mc.DeleteMulti(s.toDelete) |
| 50 if err != nil { |
| 51 log.Errorf("dscache: txn.Release DeleteMulti: %s", err) |
| 52 } |
| 53 } |
| 54 |
| 55 func (s *dsTxnState) Add(mc memcache.Interface, keys []datastore.Key) { |
| 56 lockItems, lockKeys := mkAllLockItems(mc, keys) |
| 57 if lockItems == nil { |
| 58 return |
| 59 } |
| 60 |
| 61 s.Lock() |
| 62 defer s.Unlock() |
| 63 s.toLock = append(s.toLock, lockItems...) |
| 64 s.toDelete = append(s.toDelete, lockKeys...) |
| 65 } |
OLD | NEW |