官网帮助文档例如以下

Installation

Download, extract and compile Redis with:

$ wget http://download.redis.io/releases/redis-3.0.2.tar.gz
$ tar xzf redis-3.0.2.tar.gz
$ cd redis-3.0.2
$ make

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

$ src/redis-server
Or
$ src/redis-server redis.conf

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

$ src/redis-cli
redis> set foo bar
OK
redis> 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}" /> <property
name="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=100
redis.pool.maxIdle=20
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
redis.host=10.13.3.101
redis.port=6379
redis.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; @Service
public class SysUserServiceimpl implements SysUserService { @Resource
RedisTemplate<String, SysUser> redisTemplate; public boolean addUser(final SysUser sysUser) {
boolean result = this.redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public 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 {
@Resource
SysUserService sysUserService;
@Test
public 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下调用)的更多相关文章

  1. Android下的单元測试

    android下的单元測试 在AndroidManifest.xml文件里配置下面信息: 在manifest节点下加入: <!-- 指定測试信息和要測试的包 --> <instrum ...

  2. 測试JSON RPC远程调用(JSONclient)

    #include <string> #include <iostream> #include <curl/curl.h> /* 标题:JSonclient Auth ...

  3. anaconda环境中---py2.7下安装tf1.0 + py3.5下安装tf1.5

    anaconda环境中---py2.7下安装tf1.0 + py3.5下安装tf1.5 @wp20181030 环境:ubuntu18.04, anaconda2, ubuntu系统下事先安装了pyt ...

  4. Linux下图形界面调试工具kdbg安装及測试

    1.Ubuntu系统下安装 Ubuntu系统安装比較方便,直接apt-get即可 apt-get install kdbg 2.centos 安装 首先,在这个地址下下载rpm包.然后使用rpm命令安 ...

  5. linux mysql 卸载,安装,測试全过程

    Mysql卸载 yum remove mysql mysql-server mysql-libs compat-mysql51 rm -rf /var/lib/mysql rm /etc/my.cnf ...

  6. libmemcached的安装及測试

    1.安装memcached ~$ wget http://memcached.googlecode.com/files/memcached-1.2.8.tar.gz. $ tar xvzf lmemc ...

  7. php集成环境安装与測试

    一. 软件下载 1. AppServ 2.5.10包括的软件有Apache.Apache Monitor.PHP.MySQL.PHP-Nuke.phpMyAdmin. 2. 下载地址: http:// ...

  8. 海量数据插入数据库效率对照測试 ---ADO.NET下SqlBulkCopy()对照LINQ 下InsertAllOnSubmit()

    摘要:使用.NET相关技术向数据库中插入海量数据是经常使用操作.本文对照ADO.NET和LINQ两种技术.分别使用SqlBulkCopy()和InsertAllOnSubmit()方法进行操作. 得出 ...

  9. 在linux系统下安装mysql详解,以及远程调用连接不上mysql的解决方法。

    步骤: 1)查看CentOS自带的mysql 输入 rpm -qa | grep mysql 2)将自带的mysql卸载 3)上传Mysql的安装包到linux 4)安装mysql的依赖(不是必须) ...

随机推荐

  1. Hadamard product

    按元素乘积. python中Hadamard product和matrix product的区分: For numpy.ndarray objects, * performs elementwise ...

  2. NOIP 初赛笔记

    // zj蒟蒻瑟瑟发抖.. // 停课了.要好好努力!——10月8日8:29于机房 1. 1946 年 美国 -> 第一台计算机 2. 真空电子管 -> 晶体管 -> 集成 -> ...

  3. [ZROI #316] ZYB玩字符串

    Introduction 每次在一开始为空的串$S$的任意位置插入串$p$ 给出最终的$S$,求长度最短(相同时字典序最小)的串$p$ Solution: 样例出锅差评啊,让我这种直接看样例选手挂掉5 ...

  4. LinkCutTree 总结

    最近学习了LinkCutTree,总结一下. LinkCutTree是一种数据结构(是Tree Decomposition中的一种),她维护的一般是无向图(一个森林),支持连边.删边.链修改.链查询( ...

  5. asp.net 去除数据中带有的html标签

    1,在控制器中实现去除html标签的静态方法 //去除html标签 public static string ReplaceHtmlMark(object Contents) { string Htm ...

  6. uva 6957 Hyacinth bfs

    Hyacinth Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewPro ...

  7. Oracle密码忘记了解决办法

    Oracle密码忘记了怎么办?有时候我们可能忘记了一个用户的密码,但是又需要以这个用户做一些操作,又不能去修改掉这个用户的密码,这个时候,就可以利用一些小窍门,来完成操作.采用如下方法可以修改密码: ...

  8. 安卓之service简单介绍

    一 什么是Service 二 如何使用Service 三 Service的生命周期   一 什么是Service Service,看名字就知道跟正常理解的“服务”差不多,后台运行,可交互这样的一个东西 ...

  9. ios:设置视图背景图片的方法

    1. 使用一个UIImageView实例做子视图,并且放最后面UIImageView *customBackgournd = [UIImageView alloc] initWithImage:[UI ...

  10. Java之基于Eclipse搭建SSH框架(上)

    http://blog.csdn.net/snowwitch/article/details/50925382 http://www.cnblogs.com/hww123/archive/2016/0 ...