Go語言的源碼閱讀
1、Cache的底層實現
package cache2go
import (
"sync"
"time"
)
// CacheItem is an individual cache item
// Parameter data contains the user-set value in the cache.
type CacheItem struct {
sync.RWMutex
// The item's key. 空interface(interface{})不包含任何的method,因此所有的類型都實現了空interface。
// 空interface在我們需要存儲任意類型的數值的時候相當有用,有點類似於C語言的void*類型。
key interface{}
// The item's data.
data interface{}
// How long will the item live in the cache when not being accessed/kept alive.
lifeSpan time.Duration
// Creation timestamp.
createdOn time.Time
// Last access timestamp.
accessedOn time.Time
// How often the item was accessed.
accessCount int64
// Callback method triggered right before removing the item from the cache
aboutToExpire []func(key interface{})
}
// NewCacheItem returns a newly created CacheItem.
// Parameter key is the item's cache-key.
// Parameter lifeSpan determines after which time period without an access the item
// will get removed from the cache.
// Parameter data is the item's value.
func NewCacheItem(key interface{}, lifeSpan time.Duration, data interface{}) *CacheItem {
t := time.Now()
return &CacheItem{
key: key,
lifeSpan: lifeSpan,
createdOn: t,
accessedOn: t,
accessCount: 0,
aboutToExpire: nil,
data: data,
}
}
// KeepAlive marks an item to be kept for another expireDuration period. 過期週期
func (item *CacheItem) KeepAlive() {
item.Lock()
defer item.Unlock()
item.accessedOn = time.Now()
item.accessCount++
}
// LifeSpan returns this item's expiration duration. 返回緩存項的過期時間
func (item *CacheItem) LifeSpan() time.Duration {
// immutable
return item.lifeSpan
}
// AccessedOn returns when this item was last accessed. 返回該項的最後訪問時間
func (item *CacheItem) AccessedOn() time.Time {
item.RLock()
defer item.RUnlock()
return item.accessedOn
}
// CreatedOn returns when this item was added to the cache. 返回該項的創建時間
func (item *CacheItem) CreatedOn() time.Time {
// immutable
return item.createdOn
}
// AccessCount returns how often this item has been accessed. 返回該項被訪問的次數
func (item *CacheItem) AccessCount() int64 {
item.RLock()
defer item.RUnlock()
return item.accessCount
}
// Key returns the key of this cached item. 返回該事項的key值
func (item *CacheItem) Key() interface{} {
// immutable
return item.key
}
// Data returns the value of this cached item. 返回該項的數據
func (item *CacheItem) Data() interface{} {
// immutable
return item.data
}
// SetAboutToExpireCallback configures a callback, which will be called right
// before the item is about to be removed from the cache. 設置回調函數, 該項被移除之前調用這個回調函數
func (item *CacheItem) SetAboutToExpireCallback(f func(interface{})) {
if len(item.aboutToExpire) > 0 {
//清空回調函數的隊列
item.RemoveAboutToExpireCallback()
}
item.Lock()
defer item.Unlock()
//然後追加一個回調函數到隊列中
item.aboutToExpire = append(item.aboutToExpire, f)
}
// AddAboutToExpireCallback appends a new callback to the AboutToExpire queue
// 把一個回調函數放進回調函數的隊列中
func (item *CacheItem) AddAboutToExpireCallback(f func(interface{})) {
item.Lock()
defer item.Unlock()
item.aboutToExpire = append(item.aboutToExpire, f)
}
// RemoveAboutToExpireCallback empties the about to expire callback queue 清空回調函數的隊列
func (item *CacheItem) RemoveAboutToExpireCallback() {
item.Lock()
defer item.Unlock()
item.aboutToExpire = nil
}
源碼分析點評: 經典的地方是使用了Go語言的空接口;