Using Leopard Cache

Source: Internet
Author: User
Tags assert

Use leopard cache to learn how to use leopard cache.

This guide will guide you through the cache operation using the Leopard cache.

How to complete the This guide

You can start from scratch and complete each step, or you can bypass the basic setup steps you're already familiar with. Either way, you can end up with working code.

1. Configure MAVEN Dependencies

In the DAO module, Pom.xml joins

    <dependencies>        [...]        <dependency>            <groupId>io.leopard</groupId>            <artifactid>leopard-data</ artifactid>            <version>0.0.1-SNAPSHOT</version>        </dependency>        [...]    </dependencies>    <repositories>        <repository>            <id>leopard-snapshots</id >            <name>leopard snapshots</name>            <url>http://leopard.io/nexus/content/ repositories/snapshots/</url>        </repository>    </repositories>
2. Configure Spring

src/main/resources/applicationContext-dao.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:leopard= "Http://www.leopard.io/schema/leopard" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.leopard.io/schema/leopard http://www.leopard.io/schema/leopard.xsd "><leopard: Component-scan base-package= "Io.leopard.guides"/><leopard:jdbc id= "jdbc" host= "112.126.75.27" database= " Example "user=" Example "password=" Leopard "/><leopard:redis id=" Redis "server=" 112.126.75.27:6311 "/></ Beans>
3, create Userdao interface, use to cache must implement Iget interface.

The Idelete interface is implemented directly here in order to show more interfaces

Createsrc/main/java/io/leopard/guides/dao/UserDao.java

Package Io.leopard.guides.dao;import Io.leopard.data4j.cache.api.uid.idelete;import Io.leopard.guides.model.User; Import Java.util.date;public Interface Userdao extends Idelete<user, long> {@Overridepublic boolean Add (user user) ; @OverrideUser get (long uid); @Overrideboolean Delete (long uid, long opuid, Date lmodify);}
4. Create Userdao interface MySQL implementation

Createsrc/main/java/io/leopard/guides/dao/mysql/UserDaoMysqlImpl.java

package Io.leopard.guides.dao.mysql;import Io.leopard.data4j.jdbc.Jdbc; Import Io.leopard.data4j.jdbc.builder.insertbuilder;import Io.leopard.guides.dao.userdao;import Io.leopard.guides.model.user;import Java.util.date;import Javax.annotation.resource;import Org.springframework.stereotype.Repository, @Repositorypublic class Userdaomysqlimpl implements Userdao {@ resourceprivate jdbc JDBC, @Overridepublic boolean Add (user user) {Insertbuilder builder = new Insertbuilder ("user"); Builder.setlong ("UID", User.getuid ()), Builder.setstring ("nickname", User.getnickname ()), Builder.setdate (" Posttime ", User.getposttime ()); return This.jdbc.insertForBoolean (builder);} @Overridepublic user get (Long uid) {String sql = "SELECT * from User where uid=?;"; return jdbc.query (SQL, User.class, UID);} @Overridepublic Boolean Delete (Long uid, long opuid, Date lmodify) {String sql = "Delete from user where uid=?;"; return Jdbc.updateforboolean (SQL, UID);}} 
5. Create Userdao Interface Redis implementation

Createsrc/main/java/io/leopard/guides/dao/redis/UserDaoRedisImpl.java

Package Io.leopard.guides.dao.redis;import Io.leopard.burrow.lang.json;import Io.leopard.burrow.util.NumberUtil; Import Io.leopard.data4j.redis.redis;import Io.leopard.guides.dao.userdao;import io.leopard.guides.model.User; Import Java.util.date;import Javax.annotation.resource;import org.springframework.stereotype.repository;@ Repositorypublic class Userdaoredisimpl implements Userdao {@Resourceprivate Redis Redis; @Overridepublic boolean Add ( User user) {String key = This.getkey (User.getuid ()); Redis.set (key, Json.tojson (user)); return true;} @Overridepublic User Get (Long uid) {String key = This.getkey (UID); String JSON = Redis.get (key); return Json.toobject (JSON, user.class);} @Overridepublic Boolean Delete (Long uid, long opuid, Date lmodify) {String key = This.getkey (UID); Long result = Redis.del (key); return Numberutil.tobool (result);} Protected String GetKey (long uid) {return ' User: ' + uid;}}
6. Create Userdao Interface Cache implementation

Create Src/main/java/io/leopard/guides/dao/cache/userdaocacheimpl.java

Package Io.leopard.guides.dao.cache;import Io.leopard.data4j.cache.cacheloader;import Io.leopard.guides.dao.userdao;import Io.leopard.guides.model.user;import Java.util.date;import Javax.annotation.resource;import org.springframework.stereotype.Repository; @Repositorypublic class Userdaocacheimpl implements Userdao {@Resourceprivate Userdao userdaomysqlimpl; @Resourceprivate Userdao Userdaoredisimpl; @Overridepublic boolean Add (user user) {return userdaomysqlimpl.add (user);} @Overridepublic User Get (Long uid) {///First to Userdaoredisimpl.get method, there is data to return directly//if the Redis implementation has no data, Just go to the Userdaomysqlimpl.get method to query the data. If there is data to pass the data to the Userdaoredisimpl.add method save and return. If the MySQL implementation also has no data then return Null.return cacheloader.get (Userdaoredisimpl, Userdaomysqlimpl, UID);} @Overridepublic Boolean Delete (Long uid, long opuid, Date lmodify) {userdaoredisimpl.delete (uid, opuid, lmodify); return T His.userDaoMysqlImpl.delete (UID, opuid, lmodify);}}
7. Using Userdao interface in UserService

Createsrc/main/java/io/leopard/guides/service/UserService.java

Package Io.leopard.guides.service;import Io.leopard.guides.dao.userdao;import Io.leopard.guides.model.user;import Java.util.date;import Javax.annotation.resource;import Org.springframework.stereotype.Service; @Servicepublic Class UserService {@Resource//when Xxxdaocacheimpl exists, Leopard automatically adds @primary annotation to it//this will be automatically injected userdaocacheimpl.private Userdao Userdao;public boolean Add (user user) {return this.userDao.add (user);} Public User get (long uid) {return this.userDao.get (UID);} Public boolean Delete (long uid) {return this.userDao.delete (UID, 0, New Date ());}}
8. Write UserService test code

Createsrc/test/java/io/leopard/guides/service/UserServiceTest.java

Package Io.leopard.guides.service;import Io.leopard.guides.model.user;import io.leopard.test.IntegrationTests; Import Java.util.date;import Org.junit.assert;import Org.junit.test;import Org.springframework.beans.factory.annotation.autowired;public class Userservicetest extends IntegrationTests {@ Autowiredprivate userservice userservice; @Testpublic void Get () {this.userService.delete (1);//First delete Record {User user = new User (); User.setuid (1); User.setnickname ("Leopard"); User.setposttime (new Date ()); Userservice.add (user); {User user = Userservice.get (1); Assert.assertnotnull (user); Assert.assertequals ("Leopard", User.getnickname ());}}}
Summarize

Congratulations to you! You can already configure the use of Leopard Cache, although the functionality is relatively simple, you can expand your business system on this basis, wish you good luck.

Using Leopard Cache

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.