Use Leopard Redis to operate Redis and leopardredis

Source: Internet
Author: User

Use Leopard Redis to operate Redis and leopardredis
<H1> Use Leopard Redis to operate Redis <H2> learn how to use Leopard Redis in an old project. </H2>
<P> This guide describes how to operate Redis using Leopard Redis. </P>
<H1> How to complete this guide <P> you can start from scratch and complete each step, or you can bypass the basic setup steps you are already familiar. Either way, you can finally get the code that can work. </P>
<H1> 1. Configure maven dependency <P> Add pom. xml in dao module </p>
<P> <pre name = "code" class = "java"> & lt; dependencies & gt;
[...]
& Lt; dependency & gt;
& Lt; groupId & gt; io. leopard. data4j & lt;/groupId & gt;
& Lt; artifactId & gt; data4j-redis & lt;/artifactId & gt;
& Lt; version & gt; 0.0.1-SNAPSHOT & lt;/version & gt;
& Lt;/dependency & gt;
[...]
& Lt;/dependencies & gt;
& Lt; repositories & gt;
& Lt; repository & gt;
& Lt; id & gt; leopard-snapshots & lt;/id & gt;
& Lt; name & gt; Leopard Snapshots & lt;/name & gt;
& Lt; url & gt; http://leopard.io/nexus/content/repositories/snapshots/&lt;/url&gt;
& Lt;/repository & gt;
& Lt;/repositories & gt;
</Pre>
If you are not a maven user, you can download the jar package through the following link. <br/> <a href = "http://leopard.io/download/snapshots/io/leopard/data4j/data4j-redis-0.0.1-SNAPSHOT.jar" target = "_ blank"> io. leopard. data4j: data4j-redis: 0.0.1-SNAPSHOT </a> <br/> </p>
<H1> 2. Configure spring <P> <code> src/main/resources/applicationContext-dao.xml </code> <pre name = "code" class = "java"> & lt ;? Xml version = "1.0" encoding = "UTF-8 "? & Gt;
& Lt; beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" & gt;


& Lt; bean id = "userDao" class = "io. leopard. guides. dao. UserDao"/& gt;


& Lt; bean id = "redis" class = "io. leopard. data4j. redis. RedisImpl" & gt;
& Lt; property name = "server" value = "112.126.75.27: 6311"/& gt;
& Lt; property name = "maxActive" value = "128"/& gt;
& Lt;/bean & gt;


& Lt;/beans & gt;
</Pre> </p>
<H1> 3. Use the Redis interface <P> Create <code> src/main/java/io/leopard/guides/dao/UserDao. java </code> <pre name = "code" class = "java"> package io. leopard. guides. dao;


Import io. leopard. burrow. lang. Json;
Import io. leopard. data4j. redis. Redis;
Import io. leopard. guides. model. User;


Import javax. annotation. Resource;


Public class UserDao {


@ Resource
Private Redis;


Protected String getKey (long uid ){
Return "user:" + uid;
}


/**
* Add a user.
*
* @ Param user
* @ Return returns true if the value is successfully added. If an error occurs, an exception is thrown.
*/
Public boolean add (User user ){
String key = this. getKey (user. getUid ());
String json = Json. toJson (user );
This. redis. set (key, json );
Return true;
}


/**
* Obtain user information based on uid.
*
* @ Param uid
* @ Return if the user exists, the user object is returned. If the user does not exist, null is returned.
*/
Public User get (long uid ){
String key = this. getKey (uid );
String json = this. redis. get (key );
Return Json. toObject (json, User. class );
}


/**
* Delete a user
*
* @ Param uid
* @ Return returns true if the record is successfully deleted. If the record does not exist, false is returned. If an error occurs, an exception is thrown.
*/
Public boolean delete (long uid ){
String key = this. getKey (uid );
Long result = this. redis. del (key );
Return (result! = Null & result = 1 );


}
}
</Pre> </p>
<H1> introduction of the Json parsing module <P> the Json class is used in the example code. If you want to use it in a project, configure maven dependency. </p>
<P> <pre name = "code" class = "java"> & lt; dependencies & gt;
[...]
& Lt; dependency & gt;
& Lt; groupId & gt; io. leopard. burrow & lt;/groupId & gt;
& Lt; artifactId & gt; burrow-lang & lt;/artifactId & gt;
& Lt; version & gt; 0.0.5-SNAPSHOT & lt;/version & gt;
& Lt;/dependency & gt;
[...]
& Lt;/dependencies & gt;
</Pre>
If you are not a maven user, you can download the jar package through the following link. <br/> <a href = "http://leopard.io/download/snapshots/io/leopard/burrow/burrow-lang-0.0.5-SNAPSHOT.jar" target = "_ blank"> io. leopard. burrow: burrow-lang: 0.0.5-SNAPSHOT </a> <br/> </p>
<P> learn more about Leopard functional modules, visit <a href = "http://leopard.io/"> http://leopard.io/</a> </p>
<H1> conclusion <P> congratulations! You can use Leopard Redis in the old project configuration. Although the function is relatively simple, you can expand your business system on this basis. Good luck. </P>

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.