Redis supports 5 types of data, which are described below:
Strings-String
The Redis string is a sequence of bytes. In Redis, strings are binary safe, which means they have a known length that is not determined by any special character termination, so they can store anything for up to 512 megabytes in length.
Java Add, query operation as follows:
@Test public
void TestString () {
valueoperations<string, string> ops = This.template.opsForValue ();
String key = "Helloh";
if (!this.template.haskey (key)) {
Ops.set (key, "Fodo");
Logger.warn ("Found key" + key + ", value=" + ops.get (key));
} else {
Logger.warn ("key is" + key + "exists!");
}
}
hashes-Hash value
A collection of Redis hash key value pairs. A Redis hash value is a mapping between a string field and a string value, so they are used to represent the object
Java Add, query operation as follows:
@Test public
void Testhashset () {
hashoperations<string, Object, object> ops = This.template.opsForHash ( );
String key = "user:1";
if (!this.template.haskey (key)) {
ops.put (key, "name", "Fengchao");
Ops.put (Key, "Sex", "boy");
System.out.println ("Set Succeed");
} else {
System.out.println ("key is exist");
set<object> keys = Ops.keys (key);
for (Object K:keys) {
System.out.println ("key" + key);}}}
Lists-List
The Redis list is a simple list of strings, sorted in order of insertion. You can add elements to the head or tail of the Redis list.
Java Add, query operation as follows:
@Test public
void Testlist () {
listoperations<string, string> ops = This.template.opsForList ();
String key = "name";
Ops.leftpush (Key, "Li");
if (!this.template.haskey (key)) {
Ops.leftpush (key, "Feng");
Ops.leftpush (Key, "Wang");
Ops.leftpush (Key, "Li");
System.out.println ("Set Succeed");
} else {
System.out.println ("key is exist");
Long size = ops.size (key);
list<string> list = Ops.range (key, 0, size);
for (String value:list) {
System.out.println (value);}}}
Sets-Collection
A Redis collection is an unordered collection of strings. In Redis, you can add, delete, and test a file for a member that has time complexity in O (1).
Java Add, query operation as follows:
@Test public
void Testset () {
setoperations<string, string> ops = This.template.opsForSet ();
String key = "Likes";
if (!this.template.haskey (key)) {
Long add = Ops.add (key, "sport", "eat", "play", "play");
System.out.println ("Set succeed Add is" + add);
else {
set<string> members = ops.members (key);
for (String value:members) {
System.out.println (value);}}}
Collection Sort
The collection of Redis is sorted like a Redis collection, a collection of strings that are not duplicated. The difference is that each member of an ordered set is associated with fractions, which are used in order to take an ordered set command, from the smallest to the largest fraction. Although the members are unique, the scores may be duplicated.
Java Add, query operation as follows:
@Test public
void Testzlist () {
zsetoperations<string, string> ops = This.template.opsForZSet ();
String key = "foods";
Ops.add (Key, "test", 4);
if (!this.template.haskey (key)) {
Ops.add (key, "Friute", 0);
Ops.add (Key, "Rice", 1);
Ops.add (Key, "Apple", 1);
Ops.add (Key, "Balane", 5);
System.out.println ("Set Succeed");
} else {
set<string> sets = Ops.rangebyscore (key, 0, +);
for (String value:sets) {
System.out.println (value);}}}