Getting Started with Redis

Source: Internet
Author: User

Redis is an open source API that is written in ANSI C, supports the web, can be persisted in memory, key-value databases, and provides multiple languages.

Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides clients such as Java,c/c++,c#,php,javascript,perl,object-c,python,ruby,erlang, which is convenient to use.

Redis Benefits
    • Exceptionally fast: Redis is very fast and can perform about 110,000 episodes per second, about 81000 + records per second.

    • Support for rich data types: Redis support Most developers already know like lists, collections, ordered collections, hash data types. This makes it very easy to solve a wide variety of problems because we know which issues are better than the data types that can be handled through it.

    • Operations are atomic: All Redis operations are atomic, which ensures that Redis servers accessed by two clients at the same time will get the updated values.

    • Multifunction utility: Redis is a versatile tool that can be used in multiple uses such as cache, message, queue (Redis native support publish/subscribe), any transient data, applications such as Web application sessions, Web page hits count etc.

Redis Installation

1, wget http://download.redis.io/releases/redis-2.8.12.tar.gz

2, [[email protected] ~]# tar xzf redis-2.8.12.tar.gz

[Email protected] ~]# CD redis-2.8.12

[[email protected] redis-2.8.12]# ls

3, [[email protected] redis-2.8.12]# make

If you compile an error:

    

FIX: Make cflags= "-march=i686"

4. Start: Src/redis-server

Redis use (based on my own experience with projects)

private static Jedis jedis;//non-connection Client connection
private static Jedispool jedispool;//non-connected connection pool
private static Shardedjedis shardedjedis;//Connection Client Connection
private static Shardedjedispool shardedjedispool;//connection pool

Because it is a common method of encapsulation, threading is done:

1  Public synchronizedJedis getjedisinstance () {2         Try{3             returnJedispool.getresource ();4}Catch(jedisconnectionexception e) {5 e.printstacktrace ();6 Initialpool ();7         }8         returnJedispool.getresource ();9     }Ten      One      Public synchronizedShardedjedis getshardedjedisinstance () { A         Try{ -             returnShardedjedispool.getresource (); -}Catch(Exception e) { theLogger.error ("Get Redis Connection failed! Reconnect ", e); - Initialshardedpool (); -         } -         Try{ +             returnShardedjedispool.getresource (); -}Catch(Exception e) { +Logger.error ("Get Redis Connection failed again!") ", e); A         } at         return NULL; -}

Because Redis supports these data types: String,hash,list,set and Zset (sorted set).

So I did the storage of the encapsulated object, but the object is to implement serialization (Serializable, as to what is Serializable, can own Baidu, later have the opportunity to follow the new about serialization and deserialization).

Set

  

/*** * object to implement serializable interface *@paramKey *@paramObject *@return     * @see     */     Public synchronized Booleansetobject (String key, Object object) {if(ObjectinstanceofSerializable) {Shardedjedis Shardedjedis=NULL; Try{Shardedjedis= This. Getshardedjedisinstance ();            Shardedjedis.set (Key.getbytes (), Serializeutil.serialize (object)); } Catch(Exception e) {e.printstacktrace (); } finally{shardedjedis.close (); }            return true; } Else{logger.error ("Object has no implements serializable interface"); return false; }    }

Get

 Public synchronizedObject getObject (String key) {Shardedjedis Shardedjedis=NULL; Try{Shardedjedis= This. Getshardedjedisinstance (); byte[] Value =Shardedjedis.get (Key.getbytes ()); returnserializeutil.unserialize (value); } Catch(Exception e) {e.printstacktrace (); } finally{shardedjedis.close (); }        return NULL; }

Determine if a key exists

1      Public synchronized Booleanexist (String key) {2Shardedjedis Shardedjedis =NULL;3         Try {4Shardedjedis = This. Getshardedjedisinstance ();5             if(Shardedjedis.exists (key)) {6                 return true;7}Else{8                 return false;9             }Ten}Catch(Exception e) { One e.printstacktrace (); A}finally { - shardedjedis.close (); -         } the         return false; -}

    

Getting Started with Redis

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.