Archive

Author Archive

Java learning note – Serialization 01

November 17, 2014 Leave a comment

With serialization we can stream our Java object to a sequence of byte and restore these objects from this stream of bytes. To make a Java object serializable you implement the java.io.Serializable interface. This is only a marker interface which tells the Java platform that the object is serializable.

Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object, usually we wrap them arround lower-level classes FileOutputStream. The magic happens with just two methods:
ObjectOutputStream.writeObject(); – for serialize object
ObjectInputStream.readObject(); – for deserialize object

Person.java


public class Person implements Serializable
{
private static final long serialVersionUID = -2344139368896541663L;

private String firstName;
private String lastName;
private int age;

public Person() {

}

public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

Read more…

Categories: JAVA

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…

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
Follow

Get every new post delivered to your Inbox.