MCPcopy
hub / github.com/coocood/freecache / touch

Method touch

segment.go:153–194  ·  view source on GitHub ↗
(key []byte, hashVal uint64, expireSeconds int)

Source from the content-addressed store, hash-verified

151}
152
153func (seg *segment) touch(key []byte, hashVal uint64, expireSeconds int) (err error) {
154 if len(key) > 65535 {
155 return ErrLargeKey
156 }
157
158 slotId := uint8(hashVal >> 8)
159 hash16 := uint16(hashVal >> 16)
160 slot := seg.getSlot(slotId)
161 idx, match := seg.lookup(slot, hash16, key)
162 if !match {
163 err = ErrNotFound
164 return
165 }
166 matchedPtr := &slot[idx]
167
168 var hdrBuf [ENTRY_HDR_SIZE]byte
169 seg.rb.ReadAt(hdrBuf[:], matchedPtr.offset)
170 hdr := (*entryHdr)(unsafe.Pointer(&hdrBuf[0]))
171
172 now := seg.timer.Now()
173 if isExpired(hdr.expireAt, now) {
174 seg.delEntryPtr(slotId, slot, idx)
175 atomic.AddInt64(&seg.totalExpired, 1)
176 err = ErrNotFound
177 atomic.AddInt64(&seg.missCount, 1)
178 return
179 }
180
181 expireAt := uint32(0)
182 if expireSeconds > 0 {
183 expireAt = now + uint32(expireSeconds)
184 }
185
186 originAccessTime := hdr.accessTime
187 hdr.accessTime = now
188 hdr.expireAt = expireAt
189 // in place overwrite
190 atomic.AddInt64(&seg.totalTime, int64(hdr.accessTime)-int64(originAccessTime))
191 seg.rb.WriteAt(hdrBuf[:], matchedPtr.offset)
192 atomic.AddInt64(&seg.touched, 1)
193 return
194}
195
196func (seg *segment) evacuate(entryLen int64, slotId uint8, now uint32) (slotModified bool) {
197 var oldHdrBuf [ENTRY_HDR_SIZE]byte

Callers 1

TouchMethod · 0.80

Calls 7

getSlotMethod · 0.95
lookupMethod · 0.95
delEntryPtrMethod · 0.95
isExpiredFunction · 0.85
ReadAtMethod · 0.80
WriteAtMethod · 0.80
NowMethod · 0.65

Tested by

no test coverage detected