How to use Kredis with Rails

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys this episode we're going to be talking about the credits gem and how to use it to interact with reddit's in a much cleaner way than you're probably used to so credits is a high level wrapper around your redis database that provides types and other functionality around those keys and values that you set so what's cool about credits is we can say we want to have an integer and we can give it a name and that's the key in redis and we can retrieve that integers value and will automatically typecast that for us so it's super handy to have just out of the box but there's a lot of other things that it can do it can parse json and store that in your redis database easily date times booleans but also it can do lists unique lists lists with a type of integer it can do sets it can do hashes it can do counters and cycles and enums and slots there are a lot of cool features that are available here and this is one of the nice things about the redis database but let's go and install credits and take a look at a real world example that you might use this for so we're going to run inside of our rails app we have a brand new one nothing in it no models no nothing we're going to run bundle add credits and then we will run bin rails credits install as the rake task credits install and what this is going to do is install a config folder for redis and then a shared.yaml file if we open this up we're going to see in the config redis shared.yaml file a typical database yaml style configuration for redis now credits allows you to define multiple redis databases so if you need to split this up so you have multiple you can define other yaml files in here and then just define that when you create your credits variables so let's go and take a look at this now that it's installed so what's really cool is we can have a basically a string and we can say credits and we want to give it the type and we can give it a key name and this will be like my key and s dot value is going to return that value from the key so it's going to get my key and you can see all of those commands that credits is running with redis and we can then say s equals or s dot value sorry equals hello world and s is going to set that string variable is going to set the my key to the value of hello world so when we ask for the value now it's going to get it and it will return the string but what's great is we can say credits integer and maybe we can even reuse that key and you can see what happens here what it's going to do is get that key and then convert it to an integer so because it was a string hello world 2i was called on it and returned 0 because it wasn't there wasn't any integers inside of that string so i dot value equals 5 i dot value is going to get 5 but we can still go back and say s dot value and it's going to give us the string 5. so it's doing all the typecasting on the ruby side and what's stored in redis is really just a string so all of that is very rudimentary for us as application developers we really want the types to be able to say hey we can add a number to whatever stored in redis well we don't want to have to pull that out cast it to an integer and then add to it it's much easier if we can just worry about that being an integer so this is one way to interact with credits which is fine but credits also allows you to do these types of things in your active record models which is where the real value comes from so let's go ahead and generate a model um we'll call it user we're not going to do anything fancy here we're not even going to add any fields to it so we'll say rails db migrate to create the database table for it and then we can go to the redis docs and i'll show you down here that inside of your models you can say credits underscore and the type and that will allow you to define a attribute that works the same way as that credits um variable that we created earlier so what we're going to do is we're going to go into our user.rb that we just created and we're gonna say credits unique list recent searches and we want a limit of five on here so what this is going to be useful for is if you're building um a search engine in your app and you want to display recent searches that they made then you can have this where we can add items to it and it will automatically create that list out for you but it will also trim it as you add another item on the end or the beginning so if you want to have this where you prepend all of the new items then the ending will actually drop off the number of items as necessary we also have this is a unique list so that if you were to add this multiple times do the exact same search multiple times we don't keep duplicates in there because we wouldn't really want that so let's go ahead and create let's do a route for our route and we'll say searches index and we can go into our app controllers and we can add a searches controller dot rb and we'll say class searches controller application controller index and we're going to add a private method here for faking our current user and that will be user we'll say current user null equals user dot first and so in our rails console we'll create a new user and that will have one in the database so that we will have a user to actually save those searches on so let's also go and create our views for this searches index.html.erb and inside of here we'll add a little search and then we'll add a form with the url is going to be the root or we can actually probably just make that an empty url um we'll do method is get and we'll have a little form here very simple and this will have a form dot search field with a query in it and we're going to display params query on the page so let's start up our rails app and get everything going so we can see our uh recent searches when we add the credits list so in here we can type our queries and then we can type them again and this is going to update the url correctly but it's going to only display that current search that we have here and that's not really that useful so to use credits here we can go into our searches controller and we can say current user dot recent searches and we'll prepend on params query if pram's query is present so we don't want to be pre-pending any empty items on there we're going to take that and and make sure that we're only doing that accordingly so then the other thing we want to do is add helper method here so that the current user method is available in the views and we're going to change this from displaying the query we're going to say currentuser.recentsearches.elements instead of this having a value it's got elements because it's a list instead of a single value so we're going to go through each one of those we're going to take the query and we are going to link to the root well first we're going to have the value is the query and then we're going to go to the root path with query as query and that's going to set up all of our links there at the bottom so now test is available here it got pushed to that if we search for hello world it's going to be added as the beginning of that list and then we can say um ruby we might want to search for rails and once we get into this we can say javascript and it should add this to the list but when we add our sixth item that will go over that limit it should drop off the word test and there you go so this is great but we can type ruby again it's going to then check and see hey ruby was in that list we'll remove it and then put it at the beginning so it's smart enough to keep track of duplicates like that so that if we did type something again it will prepend that to our list so this is a really really slick way of adding something like recent searches to your application it's very very little code one line of code to define that attribute in your model and then we can go and interact with that in our controllers and views very very easily now another benefit of this is because it's using redis and in memory database if anything happened where redis crashed and lost all this data it doesn't really make much difference to us in our application so that's kind of a benefit here that if we lost this it would just clear out these things and it doesn't really need to be permanently stored so redis can be very useful for those cases where things are kind of ephemeral and don't need to be fully persisted to the database and that's kind of how i decide does it go in redis or in our postgres or mysql database so this is a great example of you know a use case there so there's lots of other things that we can do we can have other types and attributes in there but i just wanted to show you this one because in our model we only have that single or user model that single line here that says hey we want to have a unique list of searches and we're going to limit that to five so that is a great simple version of uh an implementation of this because if you did this in your database you would have to have an array column of strings or whatever type and then you would have to go build the prepend and the limit stuff yourself and it's great to be able to have this built in to redis and credits so before we go i just want to show you in the rails logs you can see all of these actions as they happen so our credits uh prepend is basically going to look for every item that we're going to put in the array it's going to then take those and remove them from the existing thing in redis so it removes then it pre-pens in our case uh all of those new items and then it will trim that array to our limit as necessary so it trims it to the first five items because we are pre-pending and if we were appending it would grab the last five items and then in our view we get an l range to return that array from the starting value to the last one and it gives us that back in our view to render out so it's all very straightforward and i highly encourage you to look at the source code of this because credits is actually very very straightforward so if we want to pull up the unique list example here prepend goes through make sure that you give it a an array of unique elements if there were none in there then it's going to just skip it otherwise it's going to remove those new elements you want to add then adds them to the beginning for prepend at least and then it trims the array as necessary so all very very straightforward things and the list is actually the parent where it has the remove and prepend and append elements for you so this is a great little gem for taking a look at you know how to interact with redis if you find any bugs it's very easy to work with and test and fix so credits is just a great gem for working with redis now all of these things i kind of try to decide are they something that makes sense in redis or in our database if they must absolutely be persisted then i'll put them in the database but if they're something that could go away without any harm to the user then redis is a great option for that or they're just simple data sets that you want to work with so that's it for this episode i hope you enjoyed it and i will talk to you in the next one peace [Music]
Info
Channel: GoRails
Views: 1,324
Rating: undefined out of 5
Keywords: Ruby, Ruby on Rails, Rails, Javascript, HTML, CSS, Servers, Databases, Screencast, Tutorial, Rails 6, Action Text, Active Support, Action Mailbox, Webpacker, Active Storage, Active Record, rails testing, ruby on rails testing, ruby testing, devise, rails devise
Id: iskLOSEYitM
Channel Id: undefined
Length: 13min 53sec (833 seconds)
Published: Mon Dec 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.