Basic data types in Redis, and basic use of Redis in Spring-boot

Source: Internet
Author: User
Tags hash redis

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);}}}
	



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.