Skip to content

We all know Go is fast. Would you like to go even faster? Pair it with Redis and find out.

License

Notifications You must be signed in to change notification settings

redis-developer/redisjson-for-gophers

Repository files navigation

Redis JSON for Gophers

This project contains an example that showcases different features from the official Go Client for Redis that you can use as a reference about how to get started with the support for JSON in Redis in your Go apps. It is not intended to provide the full spectrum of what the client is capable of—but it certainly puts you on the right track.

You can run this code with an Redis instance running locally, to which you can leverage the Docker Compose code available in the project. Alternatively, you can also run this code with Redis Cloud that can be easily created using the Terraform code also available in the project.

Examples available in this project:

🟥 Movies Loading

The data model from this project is a collection of movies from the file movies.json. This file will be loaded in memory and made available within the context, which the other functions will work with. Here is an example of a movie:

{
  "title": "Blade",
  "year": 1998,
  "plot": "A half-vampire, half-mortal man becomes a protector of the mortal race, while slaying evil vampires.",
  "runningTime": 7200,
  "releaseDate": "1998-08-19T00:00:00Z",
  "rating": 7,
  "genres": [
    "Action",
    "Fantasy",
    "Horror"
  ],
  "actors": [
    "Wesley Snipes",
    "Stephen Dorff",
    "Kris Kristofferson"
  ],
  "directors": [
    "Stephen Norrington"
  ]
}

🟥 Connection Handling

Once the movies are loaded, the code will create a connection with Redis and make this connection available within the context as well.

connOpts, err := redis.ParseURL(redisConnectionURL)
if err != nil {
    panic(fmt.Errorf("failed to parse Redis URL: %w", err))
}
redisClient := redis.NewClient(connOpts)

_, err = redisClient.Ping(ctx).Result()
if err != nil {
panic(fmt.Errorf("error connecting with Redis: %w", err))
}

return context.WithValue(ctx, domain.ClientKey, redisClient)

🟥 Document Indexing

All the movies will be indexed in Redis. The example uses Redis Pipelining to index documents.

pipeline := redisClient.Pipeline()
for movieID, movie := range movies {
    movieAsJSON, err := json.Marshal(movie)
    if err != nil {
        log.Printf("Error marshaling movie into JSON: %v", err)
    }
    pipeline.JSONSet(ctx, KeyPrefix+strconv.Itoa(movieID+1), "$", string(movieAsJSON))
}

_, _ := pipeline.Exec(ctx)

🟥 Document Lookup

An example of document lookup is also available. Out of all movies loaded, an key will be randomly selected, and the document associated with this key will be looked up. Just like you would do with:

JSON.GET movie:1234 $.title

🟥 Aggregation Analytics

Finally, the project also runs a very interesting aggregation to find out the top five genres and their respective movie counts. Just like you would do with:

FT.AGGREGATE json_movies_index * GROUPBY 1 @genres REDUCE COUNT 0 AS Count SORTBY 2 @Count DESC MAX 5

🟥 Document Searches

Obviously, this project couldn't leave behind an example of a document search. The implemented search to look for all the best action movies from Keanu Reeves from 1995 to 2005.

FT.SEARCH movies_index "@actors:{Keanu Reeves} @genres:{action} @rating:[7.0 +inf] @year:[1995 2005]" RETURN 3 $.title $.year $.rating

License

This project is licensed under the Apache 2.0 License.

About

We all know Go is fast. Would you like to go even faster? Pair it with Redis and find out.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published