使用Leopard Redis操作Redis

[size=x-large]使用Leopard Redis操作Redis[/size]
[size=large]学习如何在旧项目中使用Leopard Redis。[/size]
本指南将引导您完成使用Leopard Redis操作Redis。
[size=x-large]How to complete this guide[/size]
你可以从头开始并完成每一个步骤,或者您可以绕过你已经熟悉的基本设置步骤。无论哪种方式,你最终都可以得到可工作的代码。
[size=x-large]1、配置maven依赖[/size]
在dao模块的pom.xml加入
<dependencies>
[...]
<dependency>
<groupId>io.leopard.data4j</groupId>
<artifactId>data4j-redis</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>

如果您是非maven用户,可以通过以下链接下载jar包.[url=http://leopard.io/download/snapshots/io/leopard/data4j/data4j-redis-0.0.1-SNAPSHOT.jar]io.leopard.data4j:data4j-redis:0.0.1-SNAPSHOT[/url]
[size=x-large]2、配置spring[/size]
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="io.leopard.guides.dao.UserDao" />

<bean id="redis" class="io.leopard.data4j.redis.RedisImpl">
<property name="server" value="112.126.75.27:6311" />
<property name="maxActive" value="128" />
</bean>

</beans>

[size=x-large]3、使用Redis接口[/size]
创建src/main/java/io/leopard/guides/dao/UserDao.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 redis;

protected String getKey(long uid) {
return "user:" + uid;
}

/**
* 添加用户.
*
* @param user
* @return 添加成功返回true,出错抛异常
*/
public boolean add(User user) {
String key = this.getKey(user.getUid());
String json = Json.toJson(user);
this.redis.set(key, json);
return true;
}

/**
* 根据uid获取用户信息.
*
* @param uid
* @return 用户存在则返回用户对象,不存在则返回null.
*/
public User get(long uid) {
String key = this.getKey(uid);
String json = this.redis.get(key);
return Json.toObject(json, User.class);
}

/**
* 删除用户
*
* @param uid
* @return 成功删除记录就返回true,记录不存在则返回false,出错则抛异常.
*/
public boolean delete(long uid) {
String key = this.getKey(uid);
Long result = this.redis.del(key);
return (result != null && result == 1);

}
}

[size=x-large]Json解析模块引入[/size]
例子代码中使用到Json类,如果你希望在项目中使用,需要配置maven依赖
<dependencies>
[...]
<dependency>
<groupId>io.leopard.burrow</groupId>
<artifactId>burrow-lang</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency>
[...]
</dependencies>

如果您是非maven用户,可以通过以下链接下载jar包.[url=http://leopard.io/download/snapshots/io/leopard/burrow/burrow-lang-0.0.5-SNAPSHOT.jar]io.leopard.burrow:burrow-lang:0.0.5-SNAPSHOT[/url]
了解Leopard更多功能模块,请访问[url=http://leopard.io/]http://leopard.io/[/url]
[size=xx-large]总结[/size]
恭喜你!您已经可以在旧项目配置使用Leopard Redis,虽然功能比较简单,你可以在这个基础上扩展出你的业务系统,祝您好运。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值