Overview:
- What is redis?
- Install redis
- Get to know the cli
- Create simple C# console App using redis
- Production
- Keep learning
What is redis?
Redis is what we like to call an in-memory database that offers a ridicules read and write speed. But hold on I know what a database are, here is the thing Redis is just not any kind of database it’s a non-relational database, that stores its data sets with keys. Meaning that this type of databases goes in the NoSQL basket of databases. Since we no longer have tables and relations we have to manage our data some how Redis takes care of this by using data structures, as of this writing Redis supports strings, sets, lists, hashes and sorted sets.
Install redis
We are going to be using Linux, windows and mac have a slight different procedure but this guide can help getting started. First let’s move to the correct directory.
cd /usr/local/src
Now let’s fetch the latest stable code from redis.io
sudo wget http://download.redis.io/releases/redis-2.8.3.tar.gz
Let’s uncompress that tar
sudo tar -xzvf redis-2.8.3.tar.gz
cd redis-2.8.3
Now let’s go ahead and compile the source code
sudo make
sudo make PREFIX=/usr/local/redis-2.8.3 install
Now lets make a symbolic link that points /usr/local/redis
to /usr/local/redis-2.8.3
sudo ln -s redis-2.8.3 /usr/local/redis
Let’s configure the terminal
echo 'export PATH=/usr/local/redis/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile
Now let’s create the location of the actual database
mkdir ~/redis
touch ~/redis/redis.conf
vi ~/redis/redis.conf
Now lets set the configuration for Redis
# Daemonize Redis
daemonize yes
# Log file
logfile /home/dev/redis/redis.log
# Data folder
dir /usr/local/redis
Getting to know the cli
Starting
redis-server ~/redis/redis.conf
Stopping
killall redis-server
Version
redis-server -v
Testing the CLI
redis-cli
Make sure everything is working properly
ping
Cache itemsSet the item
set bar "Hello bar";
Get the item
get bar
Exiting the cli
quit
Create simple C# console application using redis
By far one of the most popular clients to interact with Redis using C# is ServiceStack.Redis, also the code base is always getting update on their GitHub page.
Getting this to work on a Linux box can be a bit tricky start by adding nuggets to your mono develop then configure it with the proper Nuggets add inCertificates for nuggets Now just add in the package from servicestack.redis and let’s start coding
using System;
using ServiceStack.Redis;
using ServiceStack.Text;
namespace Helloredis
{
class MainClass
{
public static void Main (string[] args)
{
using(var redis = new RedisClient())
{
var redisUsers = redis.As<Person>();
var rick = new Person{id = redisUsers.GetNextSequence(), name = "Rick @ code with intent"};
var becky = new Person{id = redisUsers.GetNextSequence(), name = "Becky @ becy.com"};
redisUsers.Store(rick);
redisUsers.Store(becky);
var allThePeople = redisUsers.GetAll();
Console.WriteLine (allThePeople.Dump());
}
}
public class Person
{
public long id {get; set;}
public string name {get; set;}
}
}
}
Production
If this database is going into production I would like to send you over to Redis.IO for best practice
Keep learning
If redis is your first NOSQL database, then i would suggest going over to try redis to get used to some of the commands
I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More