使用Leopard Cache

使用Leopard Cache

學習如何使用Leopard Cache。

本指南將引導您使用Leopard Cache操做緩存。java

How to complete this guide

你能夠從頭開始並完成每個步驟,或者您能夠繞過你已經熟悉的基本設置步驟。不管哪一種方式,你最終均可以獲得可工做的代碼。mysql

一、配置maven依賴

在dao模塊的pom.xml加入redis

<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>

二、配置spring

src/main/resources/applicationContext-dao.xmlspring

<?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>

三、建立UserDao接口,用到緩存必須實現IGet接口.

此處爲了展示更多接口直接實現了IDelete接口sql

建立src/main/java/io/leopard/guides/dao/UserDao.javajson

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);

}

四、建立UserDao接口Mysql實現

建立src/main/java/io/leopard/guides/dao/mysql/UserDaoMysqlImpl.javaapi

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);
	}

}

五、建立UserDao接口Redis實現

建立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;
	}

}

六、建立UserDao接口Cache實現

建立src/main/java/io/leopard/guides/dao/cache/UserDaoCacheImpl.javaapp

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);
	}

}

七、在UserService使用UserDao接口

建立src/main/java/io/leopard/guides/service/UserService.javamaven

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());
	}
}

八、編寫UserService測試代碼

建立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,雖然功能比較簡單,你能夠在這個基礎上擴展出你的業務系統,祝您好運。

相關文章
相關標籤/搜索