Redis is an open source project providing key/value store features in a database server. This means that basically you can store in the database a value that has a given key and then retrieve the value using its key.
It supports advanced data types like strings, lists (elements sorted by insertion order), sets (unordered collection of elements) and sorted sets (collection of elements ordered by a key given by user). There are also other things supported like hashes or atomic integer incrementation. Feel free to have a look at the documentation about that.

This post will not deal with advanced aspects of Redis: this will be the subject of other articles. What is going to be described here is how to get Redis, run a server and then perform some simple operations.

Redis is honestly a famous project (5,000 followers on Github), so I am sure you will be able to install it easily whatever your environment.
For example, in the case of Archlinux, you only need to use a simple pacman command.
pacman -S redis
For CentOS:
yum install redis

But you honestly feel more what you do if you compile and install the code yourself from Github for example. So let’s get the code.
mkdir $REDIS_CODE
cd $REDIS_CODE
git init
git remote add origin https://github.com/antirez/redis.git
git fetch origin
git checkout unstable

The unstable branch is the main development branch, similar to master in the case of postgresql. You can refer to other branches for stable releases like 2.6 or 2.8.

The server code is located in folder src/, but you first need to compile the dependencies or you will get errors of the following type:
$ make
clang: error: no such file or directory: '../deps/hiredis/libhiredis.a'
clang: error: no such file or directory: '../deps/lua/src/liblua.a'
make[1]: *** [redis-server] Error 1
make: *** [all] Error 2

So here be sure to install first the dependencies as below.
cd deps
make lua hiredis linenoise

Then finalize compilation.
cd $REDIS_CODE/src
make

Finally install the binaries in a wanted folder.
make PREFIX=$REDIS_INSTALL install

Once installed, you will notice several binaries but the most important ones are redis-server (used to boot a server) and redis-cli (client to connect to a server).

In order to launch a server on default port 6379, simply launch this command, assuming that $REDIS_INSTALL is included in PATH:
redis-server

A Redis server can use a configuration file when booted, which can be specified like this:
redis-server /path/to/conf/redis.conf
There is also a template of redis.conf in the root tree of source code.

All the options of redis.conf can be specified via command line, here are some of them I find pretty useful for beginners.

  • –dir $DIR, to specify the directory where database dump file or log files are written to. The default value is “./”, so all the files are written in this case in the folder where redis-server is launched. I personally find that not really intuitive but…
  • –port $PORT_NUMBER, port number where server listens to. Default is 6379.
  • –logfile, name of file where logs are written. Default is stdout. Once again here I recommend using a clear file name combined with –dir to bring clarity to your database servers.

Then, in order to connect to the server, simply use redis-cli (see redis-cli –help for details about the options).
$ redis-cli
redis 127.0.0.1:6379>

Then you are ready to operate on your server. Let’s do here a simple get/set.
redis 127.0.0.1:6379> set foo bar
OK
redis 127.0.0.1:6379> get foo
"bar"

A last thing, I quickly wrote two scripts in case you are interested:

  • redis_compile, script that can be used to compile code, perform tests and do some other tricks
  • redis_start, script that can be used to set up a Redis cluster with master and slaves

Please note that those scripts do not have the granularity necessary for a use in production and they are only dedicated to development.

I have not yet discussed about the numerous features of Redis like things related to the cluster structure (master/slave replication, memory management), or data structures (lists, sets), but they will be covered in some future posts.

This article is made using Eclipse 3.7 and its famous plug-in dedicated to GIT called Egit.

When working on a java project, eclipse is a nice way to implement, code and debug. It is even nicer when it can be used with a version controller like GIT. Since Eclipse 3.5, GIT has its own plug-in called EGit. Let’s be honest. It rocks. The interface is well-thought, and you can manage easily your GIT repository through friendly interface. In case you get crazy with some error messages, you can still easily fallback to the good-old command terminal, and everything made will still be visible in Eclipse. That is really a nice tool

However, when starting a new Java project from an existing GIT repository, you can easily import your project by doing Files -> Import. Then in the import window, select Git -> Projects from GIT.
And in a couple of clicks, you are able to clone existing repositories, and add new projects. The problem is that you generally need to import the code as a general project, and under Eclipse it will be recognized as a Java project.

There is a workaround for this problem after importing a GIT code as a general project. You first need to modify the .project file in the project repository and modify it as below to make it a Java project.
<projectDescription>
 <name>my_project</name>
 <comment></comment>
 <projects>
 </projects>
 <buildSpec>
  <buildCommand>
   <name>org.eclipse.jdt.core.javabuilder</name>
   <arguments>
   </arguments>
  </buildCommand>
   </buildSpec>
   <natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
   </natures>
</projectDescription>

Then, restart eclipse. So now the project became a Java project, but there are still no JRE libraries.
In order to do that, modify .classpath as follows:
<classpath>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>

What remains to do, depending on the project is to add at least a source folder. This can be done by right-clicking on the project, then New -> Source Folder.
And you are done.

GIT is nice for its portability.
Here is a short memo in case you want to export a SVN (Subversion) repository into a GIT environment.

You need first to install the package git-svn and subversion.

First initialize your local repository.
mkdir foo
cd foo
git init

Then setup the repository to fetch the svn URL you are looking for. I found that the easiest solution to check out a SVN repository was by playing with the local repository configuration to get a remote svn repository.
git config --add svn-remote.$SVN_REPO.url http://url/to/check/out
git config --add svn-remote.$SVN_REPO.fetch :refs/remotes/$SVN_REPO
git svn fetch $SVN_REPO [-r$REV_NUMBER]
git svn rebase $SVN_REPO

SVN_REPO is the identifier you want to associate with your local repository.
REV_NUMBER can be added to specify a version number to check out.
What happens here is that the SVN repository is checked out as a remote branch.
~/code/test(master) $ git branch -a
* master
remotes/$SVN_REPO

Once this is done, you can use your local copy and check that in a central git repository or merge that with other works.

When SVN has to go through a proxy, it is important to set the file ~/.subversion/servers with the following options.
http-proxy-host = proxy.server.com
http-proxy-port = 8080

©2010-2013 Michael Paquier All content is ©Copyright of Otacoo.com 2010-2013. Privacy Policy - Terms of Use