// getOrExpire retrieves the object from the timestampedEntry if and only if it hasn't
// already expired. It kicks-off a go routine to delete expired objects from
// the store and sets exists=false.
197137
func (c *ExpirationCache) getOrExpire(key string) (interface{}, bool) {
	timestampedItem, exists := c.getTimestampedEntry(key)
	if !exists {
		return nil, false
	}
	if c.expirationPolicy.IsExpired(timestampedItem) {
		glog.V(4).Infof("Entry %v: %+v has expired", key, timestampedItem.obj)
		// Since expiration happens lazily on read, don't hold up
		// the reader trying to acquire a write lock for the delete.
		// The next reader will retry the delete even if this one
		// fails; as long as we only return un-expired entries a
		// reader doesn't need to wait for the result of the delete.
		go func() {
			defer runtime.HandleCrash()
			c.cacheStorage.Delete(key)
		}()
		return nil, false
	}
	return timestampedItem.obj, true
}
347627
func NewExpirationStore(keyFunc KeyFunc, expirationPolicy ExpirationPolicy) Store {
	return &ExpirationCache{
		cacheStorage:     NewThreadSafeStore(Indexers{}, Indices{}),
		keyFunc:          keyFunc,
		clock:            clock.RealClock{},
		expirationPolicy: expirationPolicy,
	}
}
854003
func (_m *HostExpirationRegistryInterface) IsExpired(_a0 string) (bool, error) {
	ret := _m.Called(_a0)

	var r0 bool
	if rf, ok := ret.Get(0).(func(string) bool); ok {
		r0 = rf(_a0)
	} else {
		r0 = ret.Get(0).(bool)
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(string) error); ok {
		r1 = rf(_a0)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
553870
func (d *Accessor) GetUnexpiredOCSPs() (ors []certdb.OCSPRecord, err error) {
	err = d.checkDB()
	if err != nil {
		return nil, err
	}

	err = d.db.Select(&ors, fmt.Sprintf(d.db.Rebind(selectAllUnexpiredOCSPSQL), sqlstruct.Columns(certdb.OCSPRecord{})))
	if err != nil {
		return nil, wrapSQLError(err)
	}

	return ors, nil
}
544123
func (m *Memory) removeExpired() int {
	removed := 0
	now := m.Clock().Now().UTC()
	for {
		if len(*m.heap) == 0 {
			break
		}
		item := m.heap.PeekEl()
		if now.Before(item.Expires) {
			break
		}
		m.heap.PopEl()
		m.tree.Delete(item)
		m.Debugf("Removed expired %v %v item.", string(item.Key), item.Expires)
		removed++
	}
	if removed > 0 {
		m.Debugf("Removed %v expired items.", removed)
	}
	return removed
}
13517
func (lru *TTLRUCache) AddWithExpire(key, val interface{}, expiration time.Time) {
	lru.lock.Lock()
	defer lru.lock.Unlock()

	if etriple, ok := lru.mapping[key]; ok {
		etriple.val = val
		lru.order.MoveToFront(&etriple.orderelement)
		lru.timeorder.MoveToBack(&etriple.timeelement)
		etriple.expiration = expiration
		return
	}

	etriple := lru.triplePool.Get().(*triple)
	etriple.key = key
	etriple.val = val
	etriple.expiration = expiration
	etriple.orderelement.Value = etriple
	etriple.timeelement.Value = etriple
	lru.order.MoveToFront(&etriple.orderelement)
	lru.timeorder.MoveToBack(&etriple.timeelement)
	lru.mapping[key] = etriple

	for lru.order.Len() > lru.capacity {
		element := lru.order.Back()
		etriple := element.Value.(*triple)
		lru.order.Remove(element)
		lru.timeorder.Remove(&etriple.timeelement)
		delete(lru.mapping, etriple.key)
		lru.triplePool.Put(etriple)
	}
}
571746
func (cache *Cache) GetWithExpiration(key []byte) (value []byte, expireAt uint32, err error) {
	hashVal := hashFunc(key)
	segID := hashVal & segmentAndOpVal
	cache.locks[segID].Lock()
	value, expireAt, err = cache.segments[segID].get(key, nil, hashVal)
	cache.locks[segID].Unlock()
	return
}
1351587
func ExpirationObject(obj *Object, rv *ObjectResults) {
	/**
	 * Okay, lets calculate Freshness/Expiration now. woo:
	 *  http://tools.ietf.org/html/rfc7234#section-4.2
	 */

	/*
	   o  If the cache is shared and the s-maxage response directive
	      (Section 5.2.2.9) is present, use its value, or

	   o  If the max-age response directive (Section 5.2.2.8) is present,
	      use its value, or

	   o  If the Expires response header field (Section 5.3) is present, use
	      its value minus the value of the Date response header field, or

	   o  Otherwise, no explicit expiration time is present in the response.
	      A heuristic freshness lifetime might be applicable; see
	      Section 4.2.2.
	*/

	var expiresTime time.Time

	if obj.RespDirectives.SMaxAge != -1 && !obj.CacheIsPrivate {
		expiresTime = obj.NowUTC.Add(time.Second * time.Duration(obj.RespDirectives.SMaxAge))
	} else if obj.RespDirectives.MaxAge != -1 {
		expiresTime = obj.NowUTC.UTC().Add(time.Second * time.Duration(obj.RespDirectives.MaxAge))
	} else if !obj.RespExpiresHeader.IsZero() {
		serverDate := obj.RespDateHeader
		if serverDate.IsZero() {
			// common enough case when a Date: header has not yet been added to an
			// active response.
			serverDate = obj.NowUTC
		}
		expiresTime = obj.NowUTC.Add(obj.RespExpiresHeader.Sub(serverDate))
	} else if !obj.RespLastModifiedHeader.IsZero() {
		// heuristic freshness lifetime
		rv.OutWarnings = append(rv.OutWarnings, WarningHeuristicExpiration)

		// http://httpd.apache.org/docs/2.4/mod/mod_cache.html#cachelastmodifiedfactor
		// CacheMaxExpire defaults to 24 hours
		// CacheLastModifiedFactor: is 0.1
		//
		// expiry-period = MIN(time-since-last-modified-date * factor, 24 hours)
		//
		// obj.NowUTC

		since := obj.RespLastModifiedHeader.Sub(obj.NowUTC)
		since = time.Duration(float64(since) * -0.1)

		if since > twentyFourHours {
			expiresTime = obj.NowUTC.Add(twentyFourHours)
		} else {
			expiresTime = obj.NowUTC.Add(since)
		}

		if debug {
			println("Now UTC: ", obj.NowUTC.String())
			println("Last-Modified: ", obj.RespLastModifiedHeader.String())
			println("Since: ", since.String())
			println("TwentyFourHours: ", twentyFourHours.String())
			println("Expiration: ", expiresTime.String())
		}
	} else {
		// TODO(pquerna): what should the default behavoir be for expiration time?
	}

	rv.OutExpirationTime = expiresTime
}
205080
func (c *MemoryCacher) Get(key string) string {
	c.lock.RLock()
	defer c.lock.RUnlock()

	item, ok := c.items[key]
	if !ok {
		return ""
	}
	if item.expire > 0 &&
		(time.Now().Unix()-item.created) >= item.expire {
		go c.Delete(key)
		return ""
	}
	return item.val
}
224282
func (db *DB) HExpire(key []byte, duration int64) (int64, error) {
	if duration <= 0 {
		return 0, errExpireValue
	}

	return db.hExpireAt(key, time.Now().Unix()+duration)
}
261187
func (m *MySQLStore) deleteExpired() error {
	var deleteStmt = "DELETE FROM " + m.table + " WHERE expires_on < NOW()"
	_, err := m.db.Exec(deleteStmt)
	return err
}
269755
func (a *Account) IsExpired() bool {
	a.mu.RLock()
	exp := a.expired
	a.mu.RUnlock()
	return exp
}
397356
func (c *LRUExpireCache) Get(key interface{}) (interface{}, bool) {
	c.lock.Lock()
	defer c.lock.Unlock()
	e, ok := c.cache.Get(key)
	if !ok {
		return nil, false
	}
	if c.clock.Now().After(e.(*cacheEntry).expireTime) {
		c.cache.Remove(key)
		return nil, false
	}
	return e.(*cacheEntry).value, true
}
932355
func (c *MemoryUsageCache) Get(key string, getter MemoryUsageQuery) (val []MemoryUsageStats, err error) {
	c.Lock()
	defer c.Unlock()
	item, ok := c.Usages[key]
	if ok {
		if item.Expired() {
			delete(c.Usages, key)
		} else {
			return item.value, nil
		}
	}
	if val, err = getter(); err != nil {
		return
	}
	c.Usages[key] = &MemoryUsageItem{
		value:  val,
		expire: c.Clock.After(c.TTL),
	}
	return
}
264374
func (ms *MemoryMsgStore) GetSequenceFromTimestamp(timestamp int64) (uint64, error) {
	ms.RLock()
	defer ms.RUnlock()

	// No message ever stored
	if ms.first == 0 {
		return 0, nil
	}
	// All messages have expired
	if ms.first > ms.last {
		return ms.last + 1, nil
	}
	if timestamp <= ms.msgs[ms.first].Timestamp {
		return ms.first, nil
	}
	if timestamp == ms.msgs[ms.last].Timestamp {
		return ms.last, nil
	}
	if timestamp > ms.msgs[ms.last].Timestamp {
		return ms.last + 1, nil
	}

	index := sort.Search(len(ms.msgs), func(i int) bool {
		return ms.msgs[uint64(i)+ms.first].Timestamp >= timestamp
	})

	return uint64(index) + ms.first, nil
}
1072061
func (db *DB) GetEntry(index int) *ExpectedExecuteFetch {
	db.mu.Lock()
	defer db.mu.Unlock()

	if index < 0 || index >= len(db.expectedExecuteFetch) {
		panic(fmt.Sprintf("index out of range. current length: %v", len(db.expectedExecuteFetch)))
	}

	return &db.expectedExecuteFetch[index]
}
649889
func (mem *Memory) Get(key string) interface{} {
	if ret, ok := mem.data[key]; ok {
		if ret.Expired.Before(time.Now()) {
			mem.deleteKey(key)
			return nil
		}
		return ret.Data
	}
	return nil
}