学习 Redis String(字符串)

学习 Redis String(字符串)

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ScanOptions.ScanOptionsBuilder;

public class TestRedisString {
	
	private static JedisConnectionFactory getJedisConnectionFactory() {
		JedisConnectionFactory jcf = new JedisConnectionFactory();
		jcf.setHostName("127.0.0.1");
		jcf.setPort(6379);
		jcf.setPassword("123");
		jcf.afterPropertiesSet();
		return jcf;
	}
	
	private static RedisConnection getRedisConnection() {
		return getJedisConnectionFactory().getConnection();
	}

	public static void main(String[] args) {
		RedisConnection rc = getRedisConnection();
		
		//PING
		String pong = rc.ping();
		System.out.println(pong);
		
		//SET
		rc.set("string".getBytes(), "testString".getBytes());
		
		//GET
		byte[] strmsg = rc.get("string".getBytes());
		System.out.println(new String(strmsg));
		
		//APPEND
		long appendLong = rc.append("string".getBytes(), "_append".getBytes());
		System.out.println(appendLong);
		
		//SETBIT
		boolean setbitBool = rc.setBit("tom".getBytes(), 104, true);
		System.out.println(setbitBool);
		
		//GETBIT
		boolean getBitBool = rc.getBit("tom".getBytes(), 104); 
		System.out.println(getBitBool);
		
		//BITCOUNT
		long bitCount = rc.bitCount("tom".getBytes());
		System.out.println(bitCount);
		
		//BITCOUNT
		long bitCountWithBeginAnd = rc.bitCount("tom".getBytes(), 0, -1);
		System.out.println(bitCountWithBeginAnd);
		
		//GETRANGE
		byte[] str = rc.getRange("string".getBytes(), 0, -1);
		System.out.println(new String(str));
		
		//DECR
		long decr = rc.decr("num".getBytes());
		System.out.println(decr);
		
		//DECRBY
		long decrby = rc.decrBy("num".getBytes(), 5);
		System.out.println(decrby);
		
		//GETSET
		byte[] getSet = rc.getSet("string".getBytes(), "redis".getBytes());
		System.out.println(new String(getSet));
		
		//INCR
		long incr = rc.incr("num".getBytes());
		System.out.println(incr);
		
		//INCRBY
		long incrBy = rc.incrBy("num".getBytes(), 5);
		System.out.println(incrBy);
		
		//INCRBYFLOAT
//		double incrByDouble = rc.incrBy("num".getBytes(), 5.5);
//		System.out.println(incrByDouble);
		
		//MGET
		List<byte[]> mGet = rc.mGet("num".getBytes(),"string".getBytes());
		for (byte[] b : mGet) {
			System.out.println(new String(b));
		}
		
		//MSET
		Map<byte[], byte[]> mSetMap = new HashMap<byte[], byte[]>();
		mSetMap.put("ma".getBytes(), "hello".getBytes());
		mSetMap.put("mb".getBytes(), "world".getBytes());
		rc.mSet(mSetMap);
		
		//MSETNX
		Map<byte[], byte[]> mSetNXMap = new HashMap<byte[], byte[]>();
		mSetNXMap.put("mc".getBytes(), "hellonx".getBytes());
		mSetNXMap.put("md".getBytes(), "worldnx".getBytes());
		rc.mSetNX(mSetNXMap);
		
		//PSETEX
		rc.pSetEx("str1".getBytes(), 99999999, "sss".getBytes());
		
		//SETEX
		rc.setEx("str2".getBytes(), 999999, "ssss".getBytes());
		
		//SETNX
		rc.setNX("str3".getBytes(), "sssfff".getBytes());
		
		//SETRANGE
		System.out.println(new String(rc.get("greeting".getBytes())));
		rc.setRange("greeting".getBytes(), "Mongodb".getBytes(), 6);
		System.out.println(new String(rc.get("greeting".getBytes())));
		
		//STRLEN
		long strlen = rc.strLen("greeting".getBytes());
		System.out.println(strlen);
		
		//SCAN
		ScanOptionsBuilder sob = ScanOptions.scanOptions().count(1).match("s*");
		ScanOptions options = sob.build();
		Cursor<byte[]> scan = rc.scan(options);
		while (scan.hasNext()) {
			byte[] key = scan.next();
			System.out.println(new String(key) + "  " + rc.type(key));
		}
	}

}

猜你喜欢

转载自injavawetrust.iteye.com/blog/2304784