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

Refuse further crosstalk from crosstalk request. #76

Closed
wants to merge 15 commits into from
Closed
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ modifications.
groupcache is a caching and cache-filling library, intended as a
replacement for memcached in many cases.

For API docs and examples, see http://godoc.org/github.com/mailgun/groupcache/v2
For API docs and examples, see http://godoc.org/github.com/modernprogram/groupcache/v2


### Modifications from original library
Expand Down Expand Up @@ -87,7 +87,7 @@ import (
"log"
"time"

"github.com/mailgun/groupcache/v2"
"github.com/modernprogram/groupcache/v2"
)

func ExampleUsage() {
Expand Down
5 changes: 2 additions & 3 deletions byteview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
)

Expand All @@ -42,10 +41,10 @@ func TestByteView(t *testing.T) {
if n := v.Copy(shortDest[:]); n != min(len(s), 1) {
t.Errorf("%s: short Copy = %d; want %d", name, n, min(len(s), 1))
}
if got, err := ioutil.ReadAll(v.Reader()); err != nil || string(got) != s {
if got, err := io.ReadAll(v.Reader()); err != nil || string(got) != s {
t.Errorf("%s: Reader = %q, %v; want %q", name, got, err, s)
}
if got, err := ioutil.ReadAll(io.NewSectionReader(v, 0, int64(len(s)))); err != nil || string(got) != s {
if got, err := io.ReadAll(io.NewSectionReader(v, 0, int64(len(s)))); err != nil || string(got) != s {
t.Errorf("%s: SectionReader of ReaderAt = %q, %v; want %q", name, got, err, s)
}
var dest bytes.Buffer
Expand Down
47 changes: 29 additions & 18 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,45 @@ import (
"syscall"
"time"

"github.com/mailgun/groupcache/v2"
"github.com/modernprogram/groupcache/v2"
)

var store = map[string]string{}

var group = groupcache.NewGroup("cache1", 64<<20, groupcache.GetterFunc(
func(ctx context.Context, key string, dest groupcache.Sink) error {
fmt.Printf("Get Called\n")
v, ok := store[key]
if !ok {
return fmt.Errorf("key not set")
} else {
if err := dest.SetBytes([]byte(v), time.Now().Add(10*time.Minute)); err != nil {
log.Printf("Failed to set cache value for key '%s' - %v\n", key, err)
return err
const purgeExpired = true

var group = groupcache.NewGroupWithWorkspace(groupcache.Options{
Workspace: groupcache.DefaultWorkspace,
Name: "cache1",
PurgeExpired: purgeExpired,
CacheBytes: 64 << 20,
Getter: groupcache.GetterFunc(
func(ctx context.Context, key string, dest groupcache.Sink) error {
fmt.Printf("Get Called\n")
v, ok := store[key]
if !ok {
return fmt.Errorf("key not set")
} else {
if err := dest.SetBytes([]byte(v), time.Now().Add(10*time.Minute)); err != nil {
log.Printf("Failed to set cache value for key '%s' - %v\n", key, err)
return err
}
}
}

return nil
},
))
return nil
},
),
})

func main() {
serverURL := flag.String("server-url", "http://localhost:8080", "server url")
addr := flag.String("addr", ":8080", "server address")
addr2 := flag.String("api-addr", ":8081", "api server address")
peers := flag.String("pool", "http://localhost:8080", "server pool list")
peers := flag.String("pool", *serverURL, "comma-separated server pool list")
flag.Parse()

p := strings.Split(*peers, ",")
pool := groupcache.NewHTTPPoolOpts(fmt.Sprintf("http://%s", *addr), &groupcache.HTTPPoolOptions{})
pool := groupcache.NewHTTPPoolOptsWithWorkspace(groupcache.DefaultWorkspace, *serverURL, &groupcache.HTTPPoolOptions{})
pool.Set(p...)

http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -87,7 +96,9 @@ func main() {
}
}()

fmt.Printf("Running...\n")
fmt.Println("Running...")
fmt.Println("Try: curl -d key=key1 -d value=value1 localhost:8081/set")
fmt.Println("Try: curl -d key=key1 localhost:8081/cache")
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
<-termChan
Expand Down
180 changes: 123 additions & 57 deletions example_pb_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 30 additions & 23 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"time"

"github.com/mailgun/groupcache/v2"
"github.com/modernprogram/groupcache/v2"
)

func ExampleUsage() {
Expand All @@ -33,28 +33,35 @@ func ExampleUsage() {
*/

// Create a new group cache with a max cache size of 3MB
group := groupcache.NewGroup("users", 3000000, groupcache.GetterFunc(
func(ctx context.Context, id string, dest groupcache.Sink) error {

// In a real scenario we might fetch the value from a database.
/*if user, err := fetchUserFromMongo(ctx, id); err != nil {
return err
}*/

user := User{
Id: "12345",
Name: "John Doe",
Age: 40,
IsSuper: true,
}

// Set the user in the groupcache to expire after 5 minutes
if err := dest.SetProto(&user, time.Now().Add(time.Minute*5)); err != nil {
return err
}
return nil
},
))
const purgeExpired = true
group := groupcache.NewGroupWithWorkspace(groupcache.Options{
Workspace: groupcache.DefaultWorkspace,
Name: "users",
PurgeExpired: purgeExpired,
CacheBytes: 3_000_000,
Getter: groupcache.GetterFunc(
func(ctx context.Context, id string, dest groupcache.Sink) error {

// In a real scenario we might fetch the value from a database.
/*if user, err := fetchUserFromMongo(ctx, id); err != nil {
return err
}*/

user := User{
Id: "12345",
Name: "John Doe",
Age: 40,
IsSuper: true,
}

// Set the user in the groupcache to expire after 5 minutes
if err := dest.SetProto(&user, time.Now().Add(time.Minute*5)); err != nil {
return err
}
return nil
},
),
})

var user User

Expand Down
Loading