Archive

Archive for the ‘Redis’ Category

Redis Learning Note – 08 Command in Redis (Sorted Set)

November 15, 2014 Leave a comment

Every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated. Sorted sets are sorted by their score in an ascending way. The same element only exists a single time, no repeated elements are permitted. The score can be modified both by ZADD that will update the element score, and as a side effect, its position on the sorted set, and by ZINCRBY that can be used in order to update the score relatively to its previous value. The current score of an element can be retrieved using the ZSCORE command, that can also be used to verify if an element already exists or not.

When multiple elements have the same score, they are ordered lexicographically (they are still ordered by score as a first key, however, locally, all the elements with the same score are relatively ordered lexicographically). The lexicographic ordering used is binary, it compares strings as array of bytes.

Read more…

Advertisements
Categories: Redis

Redis Learning Note – 07 Command in Redis (Hash)

November 15, 2014 Leave a comment

HMGET key field [field …]
Returns the values associated with the specified fields in the hash stored at key.
For every field that does not exist in the hash, a nil value is returned. Because a non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.
HDEL key field [field …]
Removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.
HLEN key
Returns the number of fields contained in the hash stored at key.
01

02

Read more…

Categories: Redis

Redis Learning Note – 06 Command in Redis (Set)

November 15, 2014 Leave a comment

Unordered collection of unique strings

SADD key member [member …]
Add the specified members to the set stored at key.
SREM key member [member …]
Remove the specified members from the set stored at key.
SCARD key
Returns the set cardinality (number of elements) of the set stored at key.
SMEMBERS key
Returns all the members of the set value stored at key.

01

Read more…

Categories: Redis

Redis Learning Note – 05 Command in Redis (List)

November 11, 2014 Leave a comment

RPUSH key value [value …]
Append one or multiple values at the tail of the list stored at key.
LPUSH key value [value …]
Prepend one or multiple values at the tail of the list stored at key.
LRANGE key start stop
Get a range of elements from a list. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on, the rightmost item is included.

RPUSH LPUSH LRANGE

RPUSH

Read more…

Categories: Redis

Redis Learning Note – 04 Command in Redis (String)

November 10, 2014 Leave a comment

In Readis, STRINGs are used to store three types of values:

  1. Byte string value.
  2. Integer value.
  3. Floating-point value.

Integers and floars can be incremented or decremented by an arbitary numeric value (integers turning into floats as necessary).

INCR key
Increment the integer value of a key by one. The string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation.
INCR

DECR key
Decrement the integer value of a key by one.
DECR

DECRBY key decrement
Decrement the integer value of a key by the given number.
DECRBY

INCRBY key increment
Increment the integer value of a key by the given amount.
INCRBY

Read more…

Categories: Redis

Redis Learning Note – 02 Create Simple Java Client

I used Jedis as redis client, maven dependency

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

Java class

public class SimpleRedisJava {
public static void main(String[] args){
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server ok");
System.out.println("Ping to server:" + jedis.ping());

jedis.set("name", "bluething");
System.out.println("Name stored in jedis server: " + jedis.get("name"));
}
}

Run

Screenshot from 2014-11-03 00:38:35

Categories: Redis

Redis Learning Note – 01 Installing

  1. Download & extract
    sudo wget http://download.redis.io/releases/redis-2.8.17.tar.gz
    cd /usr/local
    sudo tar xvzf /home/bluething/Downloads/redis-2.8.17.tar.gz
    
  2. Build
    cd redis-2.8.17/
    sudo make
    sudo make PREFIX=/usr/local/redis-2.8.17 install
    

    01. Redis Make02 Redis make install

  3. Create symbolic link
    sudo ln -s redis-2.8.17 /usr/local/redis-2.8.17
    
  4. Edit bash profile
    echo 'export PATH=/usr/local/redis-2.8.17/bin:$PATH' >> ~/.bash_profile
    source ~/.bash_profile
    
  5. Create directory to store local configuration and log file.
    mkdir /home/bluething/redis
    

    create file redis.conf, fill with

    #Daemonize redis
    daemonize yes
    #Log file
    logfile /home/bluething/redis/redis.log
    #Data folder
    dir /usr/local/redis-2.8.17
    
  6. Start redis
    redis-server /home/bluething/redis/redis.conf
    
  7. Stop redis
    killall redis-server
    
  8. redis-cli
    redis cli
    

03 Redis various command

Categories: Redis