Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in-memory caching pkg #1189

Merged
merged 10 commits into from
Mar 20, 2023
Merged

Add in-memory caching pkg #1189

merged 10 commits into from
Mar 20, 2023

Conversation

ahrav
Copy link
Collaborator

@ahrav ahrav commented Mar 19, 2023

  • Define a cache interface along w/ an in-memory implementation.
  • This will be used by the GCS source to track progress. It can also be used by any other source that needs progress tracking via a cache.
  • Adding the interface abstraction allows us to implement other types of caches if needed.

caching library used -> https://pkg.go.dev/github.com/patrickmn/go-cache?utm_source=godoc

@ahrav ahrav marked this pull request as ready for review March 20, 2023 17:50
@ahrav ahrav requested a review from a team as a code owner March 20, 2023 17:50
@ahrav ahrav requested a review from a team March 20, 2023 17:54
@ahrav ahrav changed the title Add in-memory caching lib, used by the GCS source Add in-memory caching pkg Mar 20, 2023
pkg/cache/memory/memory.go Outdated Show resolved Hide resolved
pkg/cache/memory/memory.go Show resolved Hide resolved
pkg/cache/memory/memory.go Outdated Show resolved Hide resolved
pkg/cache/memory/memory_test.go Outdated Show resolved Hide resolved
@ahrav ahrav requested review from a team March 20, 2023 21:22
@ahrav ahrav requested a review from mcastorina March 20, 2023 21:31
Copy link
Collaborator

@mcastorina mcastorina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

Comment on lines +77 to +85
// Contents returns all key-value pairs in the cache encodes as a string.
func (c *Cache) Contents() string {
items := c.c.Items()
res := make([]string, 0, len(items))
for k := range items {
res = append(res, k)
}
return strings.Join(res, ",")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (optional): If you do want to add convenience methods for actually serializing/deserializing the data, we could use strings.Replacer to escape commas (assuming it has an acceptable benchmark).

For example:

func unescape(s string) string {
	replacer := strings.NewReplacer(`\,`, ",")
	return replacer.Replace(s)
}

func escape(s string) string {
	replacer := strings.NewReplacer(",", `\,`)
	return replacer.Replace(s)
}

@ahrav ahrav merged commit 62d44f5 into main Mar 20, 2023
@ahrav ahrav deleted the add-in-memory-cache-pkg branch March 20, 2023 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants