Skip to content

Commit

Permalink
[chore] - Rename memory cache package to 'simple' for clarity (#3352)
Browse files Browse the repository at this point in the history
* rename memory to cache

* Update

* fix imports
  • Loading branch information
ahrav authored Oct 2, 2024
1 parent effee2a commit b63d6c0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
12 changes: 6 additions & 6 deletions pkg/cache/memory/memory.go → pkg/cache/simple/simple.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package memory
package simple

import (
"strings"
Expand Down Expand Up @@ -35,11 +35,11 @@ func WithPurgeInterval[T any](interval time.Duration) CacheOption[T] {
return func(c *Cache[T]) { c.purgeInterval = interval }
}

// New constructs a new in-memory cache instance with optional configurations.
// NewCache constructs a new in-memory cache instance with optional configurations.
// By default, it sets the expiration and purge intervals to 12 and 13 hours, respectively.
// These defaults can be overridden using the functional options: WithExpirationInterval and WithPurgeInterval.
func New[T any](opts ...CacheOption[T]) *Cache[T] {
return NewWithData[T](nil, opts...)
func NewCache[T any](opts ...CacheOption[T]) *Cache[T] {
return NewCacheWithData[T](nil, opts...)
}

// CacheEntry represents a single entry in the cache, consisting of a key and its corresponding value.
Expand All @@ -50,9 +50,9 @@ type CacheEntry[T any] struct {
Value T
}

// NewWithData constructs a new in-memory cache with existing data.
// NewCacheWithData constructs a new in-memory cache with existing data.
// It also accepts CacheOption parameters to override default configuration values.
func NewWithData[T any](data []CacheEntry[T], opts ...CacheOption[T]) *Cache[T] {
func NewCacheWithData[T any](data []CacheEntry[T], opts ...CacheOption[T]) *Cache[T] {
instance := &Cache[T]{expiration: defaultExpirationInterval, purgeInterval: defaultPurgeInterval}
for _, opt := range opts {
opt(instance)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package memory
package simple

import (
"fmt"
Expand All @@ -10,7 +10,7 @@ import (
)

func TestCache(t *testing.T) {
c := New[string]()
c := NewCache[string]()

// Test set and get.
c.Set("key1", "key1")
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestCache(t *testing.T) {

func TestCache_NewWithData(t *testing.T) {
data := []CacheEntry[string]{{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}}
c := NewWithData(data)
c := NewCacheWithData(data)

// Test the count.
if c.Count() != 3 {
Expand All @@ -107,7 +107,7 @@ func TestCache_NewWithData(t *testing.T) {
func setupBenchmarks(b *testing.B) *Cache[string] {
b.Helper()

c := New[string]()
c := NewCache[string]()

for i := 0; i < 500_000; i++ {
key := fmt.Sprintf("key%d", i)
Expand All @@ -118,7 +118,7 @@ func setupBenchmarks(b *testing.B) *Cache[string] {
}

func BenchmarkSet(b *testing.B) {
c := New[string]()
c := NewCache[string]()

for i := 0; i < b.N; i++ {
key := fmt.Sprintf("key%d", i)
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"google.golang.org/protobuf/types/known/anypb"

"github.com/trufflesecurity/trufflehog/v3/pkg/cache"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/memory"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/handlers"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb"
Expand Down Expand Up @@ -296,15 +296,15 @@ func (s *Source) setupCache(ctx context.Context) *persistableCache {
var c cache.Cache[string]
if s.Progress.EncodedResumeInfo != "" {
keys := strings.Split(s.Progress.EncodedResumeInfo, ",")
entries := make([]memory.CacheEntry[string], len(keys))
entries := make([]simple.CacheEntry[string], len(keys))
for i, val := range keys {
entries[i] = memory.CacheEntry[string]{Key: val, Value: val}
entries[i] = simple.CacheEntry[string]{Key: val, Value: val}
}

c = memory.NewWithData[string](entries)
c = simple.NewCacheWithData[string](entries)
ctx.Logger().V(3).Info("Loaded cache", "num_entries", len(entries))
} else {
c = memory.New[string]()
c = simple.NewCache[string]()
}

// TODO (ahrav): Make this configurable via conn.
Expand Down
8 changes: 4 additions & 4 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
"github.com/go-logr/logr"
"github.com/gobwas/glob"
"github.com/google/go-github/v63/github"
"github.com/trufflesecurity/trufflehog/v3/pkg/handlers"
"golang.org/x/exp/rand"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

"github.com/trufflesecurity/trufflehog/v3/pkg/cache"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/memory"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/giturl"
"github.com/trufflesecurity/trufflehog/v3/pkg/handlers"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sanitizer"
Expand Down Expand Up @@ -231,14 +231,14 @@ func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, so
}
s.connector = connector

s.orgsCache = memory.New[string]()
s.orgsCache = simple.NewCache[string]()
for _, org := range s.conn.Organizations {
s.orgsCache.Set(org, org)
}
s.memberCache = make(map[string]struct{})

s.filteredRepoCache = s.newFilteredRepoCache(aCtx,
memory.New[string](),
simple.NewCache[string](),
append(s.conn.GetRepositories(), s.conn.GetIncludeRepos()...),
s.conn.GetIgnoreRepos(),
)
Expand Down
4 changes: 2 additions & 2 deletions pkg/sources/github/github_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/anypb"

"github.com/trufflesecurity/trufflehog/v3/pkg/cache/memory"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb"
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestSource_Token(t *testing.T) {
repoInfoCache: newRepoInfoCache(),
}
s.Init(ctx, "github integration test source", 0, 0, false, conn, 1)
s.filteredRepoCache = s.newFilteredRepoCache(ctx, memory.New[string](), nil, nil)
s.filteredRepoCache = s.newFilteredRepoCache(ctx, simple.NewCache[string](), nil, nil)

err = s.enumerateWithApp(ctx, s.connector.(*appConnector).InstallationClient(), noopReporter())
assert.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/sources/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"google.golang.org/protobuf/types/known/anypb"
"gopkg.in/h2non/gock.v1"

"github.com/trufflesecurity/trufflehog/v3/pkg/cache/memory"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
Expand Down Expand Up @@ -417,9 +417,9 @@ func TestEnumerateUnauthenticated(t *testing.T) {
Endpoint: apiEndpoint,
Credential: &sourcespb.GitHub_Unauthenticated{},
})
s.orgsCache = memory.New[string]()
s.orgsCache = simple.NewCache[string]()
s.orgsCache.Set("super-secret-org", "super-secret-org")
//s.enumerateUnauthenticated(context.Background(), apiEndpoint)
// s.enumerateUnauthenticated(context.Background(), apiEndpoint)
s.enumerateUnauthenticated(context.Background(), noopReporter())
assert.Equal(t, 1, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-org/super-secret-repo")
Expand Down
14 changes: 7 additions & 7 deletions pkg/sources/huggingface/huggingface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"google.golang.org/protobuf/types/known/anypb"

"github.com/trufflesecurity/trufflehog/v3/pkg/cache"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/memory"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/giturl"
Expand Down Expand Up @@ -191,34 +191,34 @@ func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, so
}
s.conn = &conn

s.orgsCache = memory.New[string]()
s.orgsCache = simple.NewCache[string]()
for _, org := range s.conn.Organizations {
s.orgsCache.Set(org, org)
}

s.usersCache = memory.New[string]()
s.usersCache = simple.NewCache[string]()
for _, user := range s.conn.Users {
s.usersCache.Set(user, user)
}

//Verify ignore and include models, spaces, and datasets are valid
// Verify ignore and include models, spaces, and datasets are valid
// this ensures that calling --org <org> --ignore-model <org/model> contains the proper
// repo format of org/model. Otherwise, we would scan the entire org.
if err := s.validateIgnoreIncludeRepos(); err != nil {
return err
}

s.filteredModelsCache = s.newFilteredRepoCache(memory.New[string](),
s.filteredModelsCache = s.newFilteredRepoCache(simple.NewCache[string](),
append(s.conn.GetModels(), s.conn.GetIncludeModels()...),
s.conn.GetIgnoreModels(),
)

s.filteredSpacesCache = s.newFilteredRepoCache(memory.New[string](),
s.filteredSpacesCache = s.newFilteredRepoCache(simple.NewCache[string](),
append(s.conn.GetSpaces(), s.conn.GetIncludeSpaces()...),
s.conn.GetIgnoreSpaces(),
)

s.filteredDatasetsCache = s.newFilteredRepoCache(memory.New[string](),
s.filteredDatasetsCache = s.newFilteredRepoCache(simple.NewCache[string](),
append(s.conn.GetDatasets(), s.conn.GetIncludeDatasets()...),
s.conn.GetIgnoreDatasets(),
)
Expand Down

0 comments on commit b63d6c0

Please sign in to comment.