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

官网帮助文档如下

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

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

redis.clients

jedis

2.6.2

jar

compile

applicationContext.xml分别加上

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:config/properties/jdbc.properties

classpath:config/properties/common.properties

classpath:config/properties/log4j.properties

classpath:config/properties/redis.properties

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.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 redisTemplate;

public boolean addUser(final SysUser sysUser) {

boolean result = this.redisTemplate.execute(new RedisCallback() {

@Override

public Boolean doInRedis(RedisConnection connection) throws DataAccessException {

RedisSerializer 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() {

public String doInRedis(RedisConnection connection) throws DataAccessException {

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

}

}

运行成功

5fb3d14f421483d9f8736b8a6fecd40f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值