<commit_before>// Copyright 2022 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bbfake

import (
	"context"
	"sync"
	"time"

	"go.chromium.org/luci/common/clock"
)

type item struct {
	value    interface{}
	expireAt time.Time
}

// timedMap is a map where each item has a TTL.
//
// cleanup is done lazily.
type timedMap struct {
	m  map[string]*item
	mu sync.RWMutex
}

// set adds an item to the map with ttl.
//
// zero or negative `expiration` means never expire
func (rc *timedMap) set(ctx context.Context, key string, value interface{}, expiration time.Duration) {
	rc.mu.Lock()
	defer rc.mu.Unlock()
	i := &item{value: value}
	if expiration > 0 {
		i.expireAt = clock.Now(ctx).Add(expiration)
	}
	if rc.m == nil {
		rc.m = make(map[string]*item)
	}
	rc.m[key] = i
	rc.cleanupLocked(ctx)
}

func (rc *timedMap) get(ctx context.Context, key string) (interface{}, bool) {
	defer func() {
		rc.mu.Lock()<commit_msg>cv: follow up for comment on https://crrev.com/c/3649735<commit_after>// Copyright 2022 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bbfake

import (
	"context"
	"sync"
	"time"

	"go.chromium.org/luci/common/clock"
)

type item struct {
	value    interface{}
	expireAt time.Time
}

// timedMap is a map where each item has a TTL.
//
// cleanup is done lazily.
type timedMap struct {
	m  map[string]*item
	mu sync.RWMutex
}

// set adds an item to the map with ttl.
//
// zero or negative `expiration` means never expire
func (rc *timedMap) set(ctx context.Context, key string, value interface{}, expiration time.Duration) {
	rc.mu.Lock()
	defer rc.mu.Unlock()
	i := &item{value: value}
	if expiration > 0 {
		i.expireAt = clock.Now(ctx).Add(expiration)
	}
	if rc.m == nil {
		rc.m = make(map[string]*item)
	}
	rc.m[key] = i
	rc.cleanupLocked(ctx)
}

func (rc *timedMap) get(ctx context.Context, key string) (interface{}, bool) {
	defer func() {
		// TODO(yiwzhang): Once move to go 1.18, use Trylock for best effort clean
		// up.
		rc.mu.Lock()
{'fraction_non_alphanumeric': 0.12060466644758462, 'fraction_numerical': 0.009201445941505093, 'mean_word_length': 3.3547925608011444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2966391', 'n_tokens_mistral': 1019, 'n_tokens_neox': 984, 'n_words': 372}