A simple example of using Redis

Source: Internet
Author: User

1: Import two jar packages in a project

2:

Javabean

 PackageCom.eoopen.xmpp.loginin;Importjava.io.Serializable;Importjava.util.Date; Public classClientuserImplementsserializable{/*** Serialization*/    Private Static Final LongSerialversionuid = 2029803928604452271L;  PublicClientuser () {} PublicClientuser (String clientidentify) {Super();  This. clientidentify =clientidentify; }            /*** Rewrite hashcode * clientidentify the same as representing the same object*/@Override Public inthashcode () {Final intPrime = 31; intresult = 1; Result= Prime *result+ ((clientidentify = =NULL) ? 0: Clientidentify.hashcode ()); returnresult; } @Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj = =NULL)            return false; if(GetClass ()! =Obj.getclass ())return false; Clientuser Other=(clientuser) obj; if(Clientidentify = =NULL) {            if(Other.clientidentify! =NULL)                return false; } Else if(!clientidentify.equals (other.clientidentify))return false; return true; }        PrivateString ID; PrivateString Clientuserid; /*** Unique identifier for each client (such as format 111223_359776051195305), * This parameter needs to be assembled by the client to the server, * assembled as a uid+ device uniquely identified, * This field is eventually logged in as a client Openfi Re account, * Client login when OpenFire, the account is clientidentity, * Password and client login password consistent*/    PrivateString clientidentify; //system identification, such as eim,lbs    PrivateString serveridentify; //Register OpenFire account Time    PrivateDate Createtime; PrivateDate UpdateTime; //from which client, ios,android,pc    PrivateString ClientType;  PublicString Getclienttype () {returnClientType; }     Public voidSetclienttype (String clienttype) { This. ClientType =ClientType; }     PublicDate Getupdatetime () {returnUpdateTime; }     Public voidsetupdatetime (Date updatetime) { This. UpdateTime =UpdateTime; }     PublicString getserveridentify () {returnserveridentify; }     Public voidsetserveridentify (String serveridentify) { This. serveridentify =serveridentify; }     PublicDate Getcreatetime () {returnCreatetime; }     Public voidsetcreatetime (Date createtime) { This. Createtime =Createtime; }     PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString Getclientuserid () {returnClientuserid; }     Public voidSetclientuserid (String clientuserid) { This. Clientuserid =Clientuserid; }     PublicString getclientidentify () {returnclientidentify; }     Public voidsetclientidentify (String clientidentify) { This. clientidentify =clientidentify; }}


(2) Example

 PackageCom.eo.test.redis;ImportJava.util.Set;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.data.redis.core.SetOperations;Importorg.springframework.test.context.ContextConfiguration;Importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportCom.eoopen.xmpp.loginin.ClientUser; the @RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= "Classpath:applicationContext.xml") Public classTestzwjextendsabstractjunit4springcontexttests{@AutowiredPrivateredistemplate redistemplate; @SuppressWarnings ("Unchecked") @Test Public voidTestofset () {//Create a set queue
Setoperations<string, clientuser> opsforset =Redistemplate.opsforset (); Clientuser U1=NewClientuser ("11"); Clientuser U2=NewClientuser ("12"); Clientuser U3=NewClientuser ("13"); Clientuser U4=NewClientuser ("14"); Add 4 users to a queue
Opsforset.add ("66666666", U1); Opsforset.add ("66666666", U2); Opsforset.add ("66666666", U3); Opsforset.add ("66666666", U4);
Remove all objects from the queue
Set<ClientUser> members = opsforset.members ("66666666");
Loop through all objects in the print queue
for(Clientuser clientuser:members) {System.out.println (clientuser.getclientidentify ()); }//Delete one piece of data from the queue
Clientuser U5=NewClientuser ("14"); Members.remove (U5); for(Clientuser clientuser:members) {System.out.println (clientuser.getclientidentify ()); } Set<ClientUser> members2 = opsforset.members ("66666666"); for(Clientuser clientuser:members2) {System.out.println (clientuser.getclientidentify ()); } }}

3: Put all the data in the database into the Redis cache

 Public voidInitusercachequeue () {redistemplate redistemplate= (redistemplate) contextutil.getbean ("Redistemplate"); //emptying the collectionRedistemplate.delete (constant.openfire_users);                Redistemplate.delete (constant.client_users); Clientuserservice UserService= (Clientuserservice) contextutil.getbean ("Clientuserservice"); //put all the clientuser data into the Redis cacheList<clientuser> clientusers =Userservice.queryclientuser ();  for(Clientuser clientuser:clientusers) {Redistemplate.opsforset (). Add (Constant.client_users, ClientUser); }                //put all the ofuser data into the Redis cacheList<ofuser> opeofusers =Userservice.queryopenfireuser ();  for(Ofuser ofuser:opeofusers) {Redistemplate.opsforset (). Add (Constant.openfire_users, ofuser); }    }

A simple example of using 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.