使用spring測試Redis字符串操作

1.pom依賴

<dependencies>
  	<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
    		<groupId>redis.clients</groupId>
   			 <artifactId>jedis</artifactId>
   			 <version>2.9.0</version>
		</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
		<dependency>
   			 <groupId>org.springframework.data</groupId>
   			 <artifactId>spring-data-redis</artifactId>
		<version>1.7.2.RELEASE</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>4.2.6.RELEASE</version>
	</dependency>
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context-support</artifactId>
    	<version>4.2.6.RELEASE</version>
	</dependency>	
</dependencies>

2.配置文件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" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
	
	<!--spring的配置代碼  -->
	
	<!--首先配置JedisPoolConfig對象  -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
	<!-- 最大空閒數 -->
		<property name="maxIdle" value="100"/>
	<!-- 最大連接數 -->
		<property name="maxTotal" value="200"/>
	<!-- 最大等待時間 -->
		<property name="maxWaitMillis" value="20000"/>
	</bean>
	
	<!--配置JedisConnectionFactory  -->
	<bean id="connectionFactory" 
	class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="127.0.0.1"/>
		<property name="port" value="6379"/>
		<!-- <property name="password" value="123456"/>  -->
		<property name="poolConfig" ref="poolConfig"/>
	</bean>
	
	<!-- 配置spring RedisTemplate -->
	
	<!-- <bean id="jdkSerializationRedisSerializer" 
	class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> -->
	
	<bean id="stringRedisSerializer" 
	class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
	
	<bean id="redisTemplate"
	class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="connectionFactory"/>
		<!--鍵序列器  -->
		<property name="keySerializer" ref="stringRedisSerializer"/>
		<!-- 值序列器-->
		<property name="valueSerializer" ref="stringRedisSerializer"/>
	</bean>
</beans>

3.測試代碼,運行結果(測試之前,記得開啓redis服務)

@Test
	public void funString() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		redisTemplate.opsForValue().set("key1", "value1");
		redisTemplate.opsForValue().set("key2", "value2");
		
        //除了delete操作,其他操作都是通過opsForValue()得到valueOps去操作的。
		//通過key獲取值
		String value1 = (String) redisTemplate.opsForValue().get("key1");
		System.out.println(value1);
		
		//通過key刪除值
		redisTemplate.delete("key1");
		
		//求長度
		Long length = redisTemplate.opsForValue().size("key2");
		System.out.println(length);
		
		//設置新值返回舊值
		String oldvalue2 = (String)redisTemplate.opsForValue().getAndSet("key2", "new_value2");
		System.out.println(oldvalue2);
		
		//求子串
		String rangeValue2 = redisTemplate.opsForValue().get("key2", 0, 3);
		System.out.println(rangeValue2);
		
		//追加字符串到末尾,返回新字符串長度
		int newlength = redisTemplate.opsForValue().append("key2", "_append");
		System.out.println(newlength);

		
	}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章