本指南将引导您使用Leopard Cache操作缓存。
你可以从头开始并完成每一个步骤,或者您可以绕过你已经熟悉的基本设置步骤。无论哪种方式,你最终都可以得到可工作的代码。
在dao模块的pom.xml加入
<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>
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.xsd http://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>
此处为了展现更多接口直接实现了IDelete接口
创建src/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> { @Override public boolean add(User user); @Override User get(Long uid); @Override boolean delete(Long uid, long opuid, Date lmodify); }
创建src/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; @Repository public class UserDaoMysqlImpl implements UserDao { @Resource private Jdbc jdbc; @Override public 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); } @Override public User get(Long uid) { String sql = "select * from user where uid=?;"; return jdbc.query(sql, User.class, uid); } @Override public boolean delete(Long uid, long opuid, Date lmodify) { String sql = "delete from user where uid=?;"; return jdbc.updateForBoolean(sql, uid); } }
创建src/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; @Repository public class UserDaoRedisImpl implements UserDao { @Resource private Redis redis; @Override public boolean add(User user) { String key = this.getKey(user.getUid()); redis.set(key, Json.toJson(user)); return true; } @Override public User get(Long uid) { String key = this.getKey(uid); String json = redis.get(key); return Json.toObject(json, User.class); } @Override public 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; } }
创建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; @Repository public class UserDaoCacheImpl implements UserDao { @Resource private UserDao userDaoMysqlImpl; @Resource private UserDao userDaoRedisImpl; @Override public boolean add(User user) { return userDaoMysqlImpl.add(user); } @Override public User get(Long uid) { // 先到userDaoRedisImpl.get方法查,有数据直接返回 // 如果redis实现没有数据,就到userDaoMysqlImpl.get方法查询数据。如果有数据将数据传给userDaoRedisImpl.add方法保存并返回。如果mysql实现也没有数据则返回null. return CacheLoader.get(userDaoRedisImpl, userDaoMysqlImpl, uid); } @Override public boolean delete(Long uid, long opuid, Date lmodify) { userDaoRedisImpl.delete(uid, opuid, lmodify); return this.userDaoMysqlImpl.delete(uid, opuid, lmodify); } }
创建src/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; @Service public class UserService { @Resource // 当有xxxDaoCacheImpl存在时,Leopard自动为它增加了@Primary注解 // 这里会自动注入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()); } }
创建src/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 { @Autowired private UserService userService; @Test public void get() { this.userService.delete(1);// 先删除记录 { 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()); } } }
恭喜你!您已经可以配置使用Leopard Cache,虽然功能比较简单,你可以在这个基础上扩展出你的业务系统,祝您好运。