ItGo.me - 专注IT技术分享

首页 > Redis > redis在linux下安装并测试(在spring下调用)

redis在linux下安装并测试(在spring下调用)

时间:2016-08-28来源:网友分享 点击:

官网帮助文档如下


Installation

Download, extract and compile Redis with:

$ wget$ tar xzf$ cd redis-3.0.2$ make

The binaries that are now compiled are available in the src directory. Run Redis with:

$ src/redis-server

You can interact with Redis using the built-in client:

$ src/redis-cliredis> set foo barOKredis> get foo"bar"

当然前提是你的linux连网了,并且已经安装gcc等c,c++运行环境

然后就可以与spring整合了

maven加上

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.6.2</version><type>jar</type><scope>compile</scope></dependency>

applicationContext.xml分别加上

<!-- 属性文件位置 --><bean id="annotationPropertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:config/properties/jdbc.properties</value><value>classpath:config/properties/common.properties</value><value>classpath:config/properties/log4j.properties</value><value>classpath:config/properties/redis.properties</value></list></property></bean>

<!-- 配置redis线程池 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><!-- <property name="maxIdle" value="${redis.pool.maxIdle}" /> <propertyname="maxActive" value="${redis.pool.maxActive}" /> <property name="maxWait"value="${redis.pool.maxWait}" /> --><property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /></bean><!-- 连接redis --><bean id="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"p:pool-config-ref="poolConfig" /><!-- redis调用需要的bean --><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="connectionFactory" /></bean>
redis.properties配置如下
redis.pool.maxTotal=100redis.pool.maxIdle=20redis.pool.maxWait=1000redis.pool.testOnBorrow=trueredis.host=10.13.3.101redis.port=6379redis.password=
代码如下
package com.kugou.security.service.impl;import javax.annotation.Resource;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.stereotype.Service;import com.kugou.security.entity.SysUser;import com.kugou.security.service.SysUserService;@Servicepublic class SysUserServiceimpl implements SysUserService {@ResourceRedisTemplate<String, SysUser> redisTemplate;public boolean addUser(final SysUser sysUser) {boolean result = this.redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();byte[] key = serializer.serialize(sysUser.getId());byte[] name = serializer.serialize(sysUser.getNickname());return connection.setNX(key, name);}});return result;}public String get(final String userId) {String result = redisTemplate.execute(new RedisCallback<String>() {public String doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();byte[] key = serializer.serialize(userId);byte[] value = connection.get(key);if (value == null) {return null;}String name = serializer.deserialize(value);return name;}});return result;}}

测试如下
package test.kugou;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.transaction.TransactionConfiguration;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:config/spring/applicationContext.xml" })@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)public class BaseTest {public void testA(){}}
package test.kugou.security.dao.service.impl;import javax.annotation.Resource;import org.junit.Test;import com.kugou.security.entity.SysUser;import com.kugou.security.service.SysUserService;import test.kugou.BaseTest;public class TestSysUserService extends BaseTest {@ResourceSysUserService sysUserService;@Testpublic void testRedis(){SysUser sysUser=new SysUser();sysUser.setId("aa");sysUser.setNickname("test");boolean result=this.sysUserService.addUser(sysUser);System.out.println(result);String nickName=this.sysUserService.get("aa");System.out.println(nickName);}}

 运行成功


版权声明:本文为博主原创文章,未经博主允许不得转载。

redis在linux下安装并测试(在spring下调用)

redis在linux下安装并测试(在spring下调用)  讨论


VB.NET 操作 Redis 数据

本代码介绍在 VB.NET 环境下如何使用 Redis 来存取数据 标签: 代码片段 (3) 1. [文件] RedisExample01_demo.zip~236KB(47) 2. [文件] RedisExample01_src.zip~736KB(38) 3. [代码] [ASP/Basic]代码 跳至 Imports ServiceStack.Redis...

redis的简介、安装

一、背景介绍: redis的全称:remote dictonary server(远程字典服务器) 谁在使用redis: 国内使用redis的有:新浪微博、知乎、街旁,国外的有:GitHub 、StackOverflow 、Flicker、暴雪和Instagram redis是做什么...

使用HAProxy、PHP、Redis和MySQL支撑10亿请求每周架构细节

【编 者按】在公司的发展中,保证服务器的可扩展性对于扩大企业的市场需要具有重要作用,因此,这对架构师提出了一定的要求。Octivi联合创始人兼软件架构 师Antoni Orfin将向你介绍一个非常...

官网帮助文档如下 Installation Download, extract and compile Redis with: $ wget$ tar xzf$ cd redis-3.0.2$ make The binaries that are now compiled are available in the src directory. Run Redis with: $ src/redis-server You can interact with Redi
------分隔线----------------------------