1. 程式人生 > >Spring Data Redis 入門(二)----ValueOperations

Spring Data Redis 入門(二)----ValueOperations

ValueOperations 介面說明 :

    在org.springframework.data.redis.core包裡 點開有具體介面名稱以及說明,下面簡單談談自己理解;這個包主要就是實現對單個值進行操作,一個key 對一個Value 不能一個key對應多個值。。如果多個值 需要用到ListOperations等等。

    下面是對每個介面方法進行簡單的測試:

package com.ly;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ly.entity.Book;
import com.ly.entity.User;
/**
 * ValueOpertions 說明:只能一個鍵對應一個值.可以存放Map List等json.
 * @author ly
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext.xml" })
public class ValueOperationsTest {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	
	private ValueOperations<String, String> opsForValue=null; 
	//操作單表的Object
	private ValueOperations<String, Object> opsForValueToObject=null; 
	private BoundValueOperations<String, String> boundValueOps=null;
	
	
	@Before
	public void setUp() throws Exception {
		opsForValue=stringRedisTemplate.opsForValue();
		opsForValueToObject=redisTemplate.opsForValue();
		boundValueOps=stringRedisTemplate.boundValueOps("boundValueOps:test");
	}
	/**
	 * 指定Key 只對Key進行操作
	 */
	@Test
	public void testBoundValueOps() {
		boundValueOps.set("boundValueOps");
	}
	
	
	/**
	 *V get(Object key);
	 *void set(K key, V value);
	 *
	 */
	@Test
	public void testGetSet() {
		System.out.println(111);
		opsForValue.set("TestValueOperations:test", "test");
		System.err.println(opsForValue.get("TestValueOperations:test"));
		opsForValue.set("TestValueOperations:mkt", "test");
		opsForValue.set("boundValueOps:test", "test");
	}
	/**
	 * 設定過期時間
	 * void set(K key, V value, long timeout, TimeUnit unit);
	 * @throws InterruptedException
	 */
	@Test
	public void testSetTimeOut() throws InterruptedException {
		opsForValue.set("TestValueOperations:test", "test1", 10, TimeUnit.SECONDS);
		Thread.sleep(5000);
        System.out.println(opsForValue.get("TestValueOperations:test"));//liu2
        Thread.sleep(5000);
        System.out.println(opsForValue.get("TestValueOperations:test"));//null
	}
	
	/**
	 * void set(K key, V value, long offset);
	 */
	@Test
	public void testSetOffset() {
		opsForValue.set("TestValueOperations:ly", "liuyang");
		System.err.println(opsForValue.get("TestValueOperations:ly"));
		opsForValue.set("TestValueOperations:ly", "666", 3);
		System.err.println(opsForValue.get("TestValueOperations:ly"));//liu666g
	}
	/**
	 * Boolean setIfAbsent(K key, V value);
	 * key唯一 如果key存在 就返回false 並且無法插入
	 */
	@Test
	public void testSetIfAbsent() {
		System.out.println(opsForValue.setIfAbsent("TestValueOperations:ly1", "tss"));//true
		System.out.println(opsForValue.setIfAbsent("TestValueOperations:ly", "tss"));//false
	}
	/**
	 * 多個鍵值 封裝成map 一起進去
	 * void multiSet(Map<? extends K, ? extends V> map);
	 */
	@Test
	public void testMultiSet() {
		Map<String, String> map=new HashMap<String, String>();
		map.put("MultiSet:test1", "1");
		map.put("MultiSet:test2", "2");
		map.put("MultiSet:test3", "3");
		opsForValue.multiSet(map);
	}
	/**
	 * List<V> multiGet(Collection<K> keys);
	 * out 1,2,3
	 */
	@Test
	public void testMultiGet() {
		List<String> list=new ArrayList<String>();
		list.add("MultiSet:test1");
		list.add("MultiSet:test2");
		list.add("MultiSet:test3");
		for (String string : opsForValue.multiGet(list)) {
			System.out.println(string);
		}
	}
	/**
	 * Boolean multiSetIfAbsent(Map<? extends K, ? extends V> map);
	 * 只要map中的key存在就返回false 也不會插入
	 */
	@Test
	public void testMultiSetIfAbsent() {
		Map<String,String> map=new HashMap<String, String>();
		map.put("MultiSet:test2", "MultiSetIfAbsent2");
		map.put("MultiSet:test3", "MultiSetIfAbsent3");
		map.put("MultiSet:test4", "MultiSetIfAbsent4");
		System.out.println(opsForValue.multiSetIfAbsent(map));//false
		System.out.println(opsForValue.get("MultiSet:test2"));//2
		System.out.println(opsForValue.get("MultiSet:test4"));//null
		
	}
	/**
	 * V getAndSet(K key, V value);
	 */
	@Test
	public void testGetAndSet() {
		//key  TestValueOperations:ly  Value  liu666g
		opsForValue.getAndSet("TestValueOperations:ly", "GetAndSet");
		System.out.println(opsForValue.get("TestValueOperations:ly"));
	}
	/**
	 * Long increment(K key, long delta); Long 加減 增長
	 */
	@Test
	public void testIncrement() {
		opsForValue.set("increment:test1", "1");
		System.out.println(opsForValue.get("increment:test1"));//1
		System.out.println(opsForValue.increment("increment:test1", 2L));//3
	}
	/**
	 * Double increment(K key, double delta);
	 */
	@Test
	public void testIncrementForDouble() {
		opsForValue.set("increment:test2", "1");
		System.out.println(opsForValue.get("increment:test2"));//1
		System.out.println(opsForValue.increment("increment:test2", 3.33));//3
	}
	/**
	 * Integer append(K key, String value);
	 * 如果key已經存在並且是一個字串,則該命令將該值追加到字串的末尾。如果鍵不存在,
	 * 則它被建立並設定為空字串,因此APPEND在這種特殊情況下將類似於SET。
	 */
	@Test
	public void testAppend() {
		opsForValue.append("append:test1", "test1");
		System.out.println(opsForValue.get("append:test1"));//test1
		opsForValue.set("append:test2", "liu12");
		opsForValue.append("append:test2", "test2");
		System.out.println(opsForValue.get("append:test2"));//liu12test2
	}
	
	
	/**
	 * Long size(K key);
	 */
	@Test
	public void testSize() {
		System.out.println(opsForValue.size("TestValueOperations:ly"));//Value For liu666g  輸出7
	}
	/**
	 * Boolean setBit(K key, long offset, boolean value);
	 * 這個偏移有啥用
	 *  //true為1,false為0
 		//對 key 所儲存的字串值,設定或清除指定偏移量上的位(bit)
        //key鍵對應的值value對應的ASCII碼,在offset的位置(從左向右數)變為value

	 */
	@Test
	public void testSetBit() {
		opsForValue.set("TestSetBit:ly", "TEST");
		System.out.println(opsForValue.setBit("TestSetBit:ly", 4, true));//true
		System.out.println(opsForValue.get("TestSetBit:ly"));//lmu15
	        for(int i = 0 ; i<"TEST".length()*8;i++){
	            if(opsForValue.getBit("TestSetBit:ly", i)){
	                System.out.print(1);
	            }else{
	                System.out.print(0);
	            }
	        }
	}
	
	
	
	/**
	 *  Boolean delete(K key)
	 *  Long delete(Collection<K> keys)
	 *  
	 */
	@Test
	public void testDelete() {
		//單條記錄
		System.out.println(stringRedisTemplate.delete("TestValueOperations?test"));
		List<String> list=new ArrayList<String>();
		list.add("TestValueOperations:test");
		System.out.println(stringRedisTemplate.delete(list));
	}
	
	/************************************************************操作List Map等Json*********************************************************************/
	public User userConfig() {
		Random rand = new Random();
		 UUID uuid = UUID.randomUUID(); 
		User user=new User();
		user.setId(rand.nextLong()+1);
		user.setName(uuid.toString());
		List<Book>books=new ArrayList<Book>();
		Book book=new Book();
		book.setBookName(uuid.toString());
		book.setId(rand.nextLong()+1);
		books.add(book);
		user.setBooks(books);
		return user;
	}
	
	@Test
	public void setObject() {
		opsForValueToObject.set("opsForValue:setObject", userConfig());
	}
	
	
	
	
	
}
如果有不對的地方歡迎指正