zoukankan      html  css  js  c++  java
  • 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
    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);
    	}
    
    }
    

     执行成功



  • 相关阅读:
    Educational Codeforces Round 20 D. Magazine Ad
    Educational Codeforces Round 20 C. Maximal GCD
    紫书第三章训练2 暴力集
    Educational Codeforces Round 20 B. Distances to Zero
    Educational Codeforces Round 20 A. Maximal Binary Matrix
    紫书第三章训练1 D
    紫书第一章训练1 D -Message Decoding
    HAZU校赛 Problem K: Deadline
    Mutual Training for Wannafly Union #8 D
    紫书第三章训练1 E
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6727462.html
Copyright © 2011-2022 走看看