5 type intentStruct struct {
10 type intentMapStruct struct {
12 intentLock sync.Mutex // used by the Intent function
14 m map[string]*intentStruct
17 var intentMap = &intentMapStruct{
18 m: map[string]*intentStruct{},
21 // Register a channel to be notified when the specified keys are notified.
22 // Returns whether the caller was the first to register intent for the given
25 // Cache checks should be done after registering intent, since it's possible to
26 // register Intent while a Notify is running, and the Notify is done after
28 func (m *intentMapStruct) Intent(ch chan Cacheable, keys ...cacheKey) bool {
29 key := cachePath(keys...)
32 defer m.intentLock.Unlock()
42 s.chs = append(s.chs, ch)
46 // key might have been deleted while only the struct itself was locked -- recheck
54 // Locks the requested keys and return the locked intentStruct.
56 // The intentStruct can be directly unlocked, or given to Free to also
57 // remove it from the intent map.
58 func (m *intentMapStruct) LockIntent(keys ...cacheKey) *intentStruct {
62 return m._lockIntent(keys...)
65 func (m *intentMapStruct) _lockIntent(keys ...cacheKey) *intentStruct {
66 s, ok := m.m[cachePath(keys...)]
75 // Removes the given intent from the intent map and unlocks the intentStruct.
76 func (m *intentMapStruct) Free(is *intentStruct, keys ...cacheKey) {
83 func (m *intentMapStruct) _free(is *intentStruct, keys ...cacheKey) {
84 // deletes the key before unlocking, Intent needs to recheck key status
85 delete(m.m, cachePath(keys...))
86 // better than unlocking then deleting -- could delete a "brand new" entry
90 // Notifies and closes all channels that are listening for the specified keys;
91 // also removes them from the intent map.
93 // Should be called after setting the cache.
94 func (m *intentMapStruct) NotifyClose(v Cacheable, keys ...cacheKey) {
98 is := m._lockIntent(keys...)
99 defer m._free(is, keys...)
104 // Closes all channels that are listening for the specified keys
105 // and removes them from the intent map.
106 func (m *intentMapStruct) Close(keys ...cacheKey) {
110 is := m._lockIntent(keys...)
111 defer m._free(is, keys...)
116 // Notifies all channels that are listening for the specified keys,
117 // but doesn't close or remove them from the intent map.
118 func (m *intentMapStruct) Notify(v Cacheable, keys ...cacheKey) {
122 is := m._lockIntent(keys...)
128 // NOTE: does not lock the stuct
129 func (s *intentStruct) Notify(v Cacheable) {
130 for _, ch := range s.chs {
135 // NOTE: does not lock the struct
136 func (s *intentStruct) Close() {
137 for _, ch := range s.chs {
143 // NOTE: does not lock the struct
144 func (s *intentStruct) NotifyClose(v Cacheable) {
145 for _, ch := range s.chs {