### Get Value from Galaxy Cache in Go Source: https://github.com/vimeo/galaxycache/blob/master/README.md Shows how to retrieve data from a Galaxy cache using a specified Codec, handle potential errors during retrieval, and properly shut down the Universe's connections. ```go // Create a Codec for unmarshaling data into your format of choice - the package includes // implementations for []byte and string formats, and the protocodec subpackage includes the // protobuf adapter sCodec := StringCodec{} // Call Get on the Galaxy to retrieve data and unmarshal it into your Codec ctx := context.Background() err := g.Get(ctx, "my-key", &sCodec) if err != nil { // handle if Get returns an error } // Shutdown all open connections between peers before killing the process u.Shutdown() ``` -------------------------------- ### Galaxy Get() Operation Flow Source: https://github.com/vimeo/galaxycache/blob/master/README.md Describes the sequence of operations when a Get() request is made for a key within a Galaxy cache. It covers local cache checks, peer delegation, authoritative peer handling, and data unmarshaling. ```APIDOC Get() Operation Flow: 1. Check local cache (maincache, hotcache) in the calling process (Process_A). 2. On cache miss, PeerPicker delegates to the authoritative peer. 3. If Process_A is authoritative: - Uses BackendGetter to retrieve data. - Populates local maincache. 4. If Process_A is NOT authoritative: - Process_A calls Fetch() on the authoritative remote peer (Process_B) via FetchProtocol. - Process_B performs a Get() to check its local cache or uses BackendGetter. - Process_B populates its maincache. - Process_B serves data back to Process_A. - Process_A determines if data is hot enough for hotcache promotion. - If hot, Process_A's hotcache is populated. 5. Data is unmarshaled using the Codec provided to Get(). ``` -------------------------------- ### Initialize Peer and Universe in Go Source: https://github.com/vimeo/galaxycache/blob/master/README.md Demonstrates initializing a peer with gRPC or HTTP protocols, creating a Universe, setting peer addresses, defining a BackendGetter for data retrieval, creating a Galaxy, and registering the Universe to handle incoming fetch requests via gRPC and HTTP. ```go // Generate the protocol for this peer to Fetch from others with (package includes HTTP and gRPC) grpcProto := NewGRPCFetchProtocol(grpc.WithInsecure()) // HTTP protocol as an alternative (passing the nil argument ensures use of the default basepath // and opencensus Transport as an http.RoundTripper) httpProto := NewHTTPFetchProtocol(nil) // Create a new Universe with the chosen peer connection protocol and the URL of this process u := NewUniverse(grpcProto, "my-url") // Set the Universe's list of peer addresses for the distributed cache u.Set("peer1-url", "peer2-url", "peer3-url") // Define a BackendGetter (here as a function) for retrieving data getter := GetterFunc(func(ctx context.Context, key string, dest Codec) error { // Define your method for retrieving non-cached data here, i.e. from a database }) // Create a new Galaxy within the Universe with a name, the max capacity of cache space you would // like to allocate, and your BackendGetter g := u.NewGalaxy("galaxy-1", 1 << 20, getter) // In order to receive Fetch requests from peers over HTTP or gRPC, we must register this universe // to handle those requests // gRPC Server registration (note: you must create the server with an ocgrpc.ServerHandler for // opencensus metrics to propogate properly) grpcServer := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{})) RegisterGRPCServer(u, grpcServer) // HTTP Handler registration (passing nil for the second argument will ensure use of the default // basepath, passing nil for the third argument will ensure use of the DefaultServeMux wrapped // by opencensus) RegisterHTTPHandler(u, nil, nil) // Refer to the http/grpc godocs for information on how to serve using the registered HTTP handler // or gRPC server, respectively: // HTTP: https://golang.org/pkg/net/http/#Handler // gRPC: https://godoc.org/google.golang.org/grpc#Server ``` -------------------------------- ### HTTP Serving with GalaxyCache Source: https://github.com/vimeo/galaxycache/blob/master/README.md Explains how to register an HTTP handler to serve requests for a GalaxyCache instance, integrating with the Universe container. ```APIDOC HTTP Serving: - **HTTPHandler**: A handler for serving HTTP requests. - **RegisterHTTPHandler**: A function to register the HTTP handler with an associated Universe. This function manages the serving of HTTP requests, interacting with the Universe to fulfill cache lookups. ``` -------------------------------- ### Galaxy Cache Construction Options Source: https://github.com/vimeo/galaxycache/blob/master/README.md Details the variadic options available when constructing a Galaxy instance, allowing customization of promotion logic and cache sizing. ```APIDOC Galaxy Construction Options: Galaxy instances can be constructed with variadic options to override default behaviors, including: - **Custom Promoter**: Provide an implementation of the `Promoter.Interface` to define custom logic for determining whether a key should be added to the hotcache. - **Max Candidates**: Configure the maximum number of keys to track in the candidate cache. - **Relative Hotcache Size**: Specify the relative size of the hotcache compared to the main cache. ``` -------------------------------- ### gRPC Support for Peer Communication Source: https://github.com/vimeo/galaxycache/blob/master/README.md Details the integration of gRPC for efficient peer-to-peer communication within the GalaxyCache system. ```APIDOC gRPC Support: - **gRPC Integration**: GalaxyCache supports gRPC for enhanced connection efficiency between peers. - **RegisterGRPCServer**: A function provided to register the gRPC server, enabling communication over gRPC channels. ``` -------------------------------- ### GalaxyCache API and Architecture Changes Source: https://github.com/vimeo/galaxycache/blob/master/README.md Summarizes the significant API and architectural modifications introduced in GalaxyCache compared to groupcache, focusing on usability, testing, and new features. ```APIDOC API and Architecture Changes: - **API Overhaul**: Improved usability and configurability. - **Testing Improvements**: Removed global state for multithreaded testing. - **Connection Efficiency**: Added gRPC support for peer communication. - **Hotcache Logic**: Introduced Promoter.Interface for configurable key promotion. - **Genericity**: Replaced Sink with Codec marshaler interface; removed Byteview. - **Renamed Types**: `Group` -> `Galaxy`, `Getter` -> `BackendGetter`, `Get` -> `Fetch` (for RemoteFetcher). - **PeerPicker Rework**: Now a struct containing FetchProtocol, RemoteFetchers, peer address map, and self URL. - **No Global State**: Implemented Universe container for Galaxies and PeerPicker, removing global variables. - **Fetching Structure**: Added HTTPHandler and RegisterHTTPHandler for HTTP serving; added gRPC support with RegisterGRPCServer. - **Hotcache Enhancements**: Default promotion logic uses key access statistics; added candidate cache; variadic options for Galaxy construction (custom promoter, cache sizes). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.