Spring整合Redis完整实例代码
程序员文章站
2024-02-29 22:04:34
做过大型软件系统的同学都知道,随着系统数据越来越庞大,越来越复杂,随之带来的问题就是系统性能越来越差,尤其是频繁操作数据库带来的性能损耗更为严重。很多业绩大牛为此提出了众多...
做过大型软件系统的同学都知道,随着系统数据越来越庞大,越来越复杂,随之带来的问题就是系统性能越来越差,尤其是频繁操作数据库带来的性能损耗更为严重。很多业绩大牛为此提出了众多的解决方案和开发了很多框架以优化这种频繁操作数据库所带来的性能损耗,其中,尤为突出的两个缓存服务器是memcached和redis。今天,我们不讲memcached和redis本身,这里主要为大家介绍如何使spring与redis整合。
1、pom构建
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.x.redis</groupid> <artifactid>springredis</artifactid> <version>0.0.1-snapshot</version> <dependencies> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-redis</artifactid> <version>1.0.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>3.1.2.release</version> <scope>test</scope> </dependency> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>2.1.0</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </project>
2、spring配置文件(applicationcontext.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:redis.properties" /> <bean id="poolconfig" class="redis.clients.jedis.jedispoolconfig"> <property name="maxidle" value="${redis.maxidle}" /> <property name="maxactive" value="${redis.maxactive}" /> <property name="maxwait" value="${redis.maxwait}" /> <property name="testonborrow" value="${redis.testonborrow}" /> </bean> <bean id="connectionfactory" class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolconfig"/> <bean id="redistemplate" class="org.springframework.data.redis.core.stringredistemplate"> <property name="connectionfactory" ref="connectionfactory" /> </bean> <bean id="userdao" class="com.lyz.dao.impl.userdaoimpl" /> </beans>
3、redis.properties
# redis settings redis.host=192.168.157.130 redis.port=6379 redis.pass=liuyazhuang redis.maxidle=300 redis.maxactive=600 redis.maxwait=1000 redis.testonborrow=true
4、user实体类
package com.lyz.entity; import java.io.serializable; /** * user实体类 * @author liuyazhuang * */ public class user implements serializable { private static final long serialversionuid = -6011241820070393952l; private string id; private string name; private string password; public user() { } public user(string id, string name, string password) { super(); this.id = id; this.name = name; this.password = password; } /** * 获得id * @return the id */ public string getid() { return id; } /** * 设置id * @param id the id to set */ public void setid(string id) { this.id = id; } /** * 获得name * @return the name */ public string getname() { return name; } /** * 设置name * @param name the name to set */ public void setname(string name) { this.name = name; } /** * 获得password * @return the password */ public string getpassword() { return password; } /** * 设置password * @param password the password to set */ public void setpassword(string password) { this.password = password; } }
5、user操作的接口iuserdao
package com.lyz.dao; import java.util.list; import com.lyz.entity.user; /** * user操作接口 * @author liuyazhuang * */ public interface iuserdao { /** * 新增 * @param user * @return */ boolean add(user user); /** * 批量新增 使用pipeline方式 * @param list * @return */ boolean add(list<user> list); /** * 删除 * @param key */ void delete(string key); /** * 删除多个 * @param keys */ void delete(list<string> keys); /** * 修改 * @param user * @return */ boolean update(user user); /** * 通过key获取 * @param keyid * @return */ user get(string keyid); }
6、基本的抽象类
package com.lyz.dao.impl; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.serializer.redisserializer; /** * 基本的抽象类 * @author liuyazhuang * * @param <k> * @param <v> */ public abstract class abstractbaseredisdao<k, v> { @autowired protected redistemplate<k, v> redistemplate; /** * 设置redistemplate * @param redistemplate the redistemplate to set */ public void setredistemplate(redistemplate<k, v> redistemplate) { this.redistemplate = redistemplate; } /** * 获取 redisserializer * <br>------------------------------<br> */ protected redisserializer<string> getredisserializer() { return redistemplate.getstringserializer(); } }
7、iuserdao的实现类userdaoimpl
package com.lyz.dao.impl; import java.util.arraylist; import java.util.list; import org.springframework.dao.dataaccessexception; import org.springframework.data.redis.connection.redisconnection; import org.springframework.data.redis.core.rediscallback; import org.springframework.data.redis.serializer.redisserializer; import org.springframework.util.assert; import com.lyz.dao.iuserdao; import com.lyz.entity.user; /** * 接口的实现类 * @author liuyazhuang * */ public class userdaoimpl extends abstractbaseredisdao<string, user> implements iuserdao { /** * 新增 * @param user * @return */ public boolean add(final user user) { boolean result = redistemplate.execute(new rediscallback<boolean>() { public boolean doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> serializer = getredisserializer(); byte[] key = serializer.serialize(user.getid()); byte[] name = serializer.serialize(user.getname()); return connection.setnx(key, name); } }); return result; } /** * 批量新增 使用pipeline方式 *@param list *@return */ public boolean add(final list<user> list) { assert.notempty(list); boolean result = redistemplate.execute(new rediscallback<boolean>() { public boolean doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> serializer = getredisserializer(); for (user user : list) { byte[] key = serializer.serialize(user.getid()); byte[] name = serializer.serialize(user.getname()); connection.setnx(key, name); } return true; } }, false, true); return result; } /** * 删除 * @param key */ public void delete(string key) { list<string> list = new arraylist<string>(); list.add(key); delete(list); } /** * 删除多个 * @param keys */ public void delete(list<string> keys) { redistemplate.delete(keys); } /** * 修改 * @param user * @return */ public boolean update(final user user) { string key = user.getid(); if (get(key) == null) { throw new nullpointerexception("数据行不存在, key = " + key); } boolean result = redistemplate.execute(new rediscallback<boolean>() { public boolean doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> serializer = getredisserializer(); byte[] key = serializer.serialize(user.getid()); byte[] name = serializer.serialize(user.getname()); connection.set(key, name); return true; } }); return result; } /** * 通过key获取 * @param keyid * @return */ public user get(final string keyid) { user result = redistemplate.execute(new rediscallback<user>() { public user doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> serializer = getredisserializer(); byte[] key = serializer.serialize(keyid); byte[] value = connection.get(key); if (value == null) { return null; } string name = serializer.deserialize(value); return new user(keyid, name, null); } }); return result; } }
8、测试类redistest
package com.lyz.test; import java.util.arraylist; import java.util.list; import junit.framework.assert; import org.junit.test; import org.springframework.beans.factory.annotation.autowired; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.abstractjunit4springcontexttests; import com.lyz.dao.iuserdao; import com.lyz.entity.user; /** * redis测试类 * @author liuyazhuang * */ @contextconfiguration(locations = {"classpath*:applicationcontext.xml"}) public class redistest extends abstractjunit4springcontexttests { @autowired private iuserdao userdao; /** * 新增 */ @test public void testadduser() { user user = new user(); user.setid("user1"); user.setname("liuyazhuang"); boolean result = userdao.add(user); assert.asserttrue(result); } /** * 批量新增 普通方式 */ @test public void testaddusers1() { list<user> list = new arraylist<user>(); for (int i = 10; i < 50000; i++) { user user = new user(); user.setid("user" + i); user.setname("liuyazhuang" + i); list.add(user); } long begin = system.currenttimemillis(); for (user user : list) { userdao.add(user); } system.out.println(system.currenttimemillis() - begin); } /** * 批量新增 pipeline方式 */ @test public void testaddusers2() { list<user> list = new arraylist<user>(); for (int i = 10; i < 1500000; i++) { user user = new user(); user.setid("user" + i); user.setname("liuyazhuang" + i); list.add(user); } long begin = system.currenttimemillis(); boolean result = userdao.add(list); system.out.println(system.currenttimemillis() - begin); assert.asserttrue(result); } /** * 修改 */ @test public void testupdate() { user user = new user(); user.setid("user1"); user.setname("liuyazhuang"); boolean result = userdao.update(user); assert.asserttrue(result); } /** * 通过key删除单个 */ @test public void testdelete() { string key = "user1"; userdao.delete(key); } /** * 批量删除 */ @test public void testdeletes() { list<string> list = new arraylist<string>(); for (int i = 0; i < 10; i++) { list.add("user" + i); } userdao.delete(list); } /** * 获取 */ @test public void testgetuser() { string id = "user1"; user user = userdao.get(id); assert.assertnotnull(user); assert.assertequals(user.getname(), "liuyazhuang"); } /** * 设置userdao * @param userdao the userdao to set */ public void setuserdao(iuserdao userdao) { this.userdao = userdao; } }
9、温馨提示
项目下载地址:spring-redis_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。