学习 Redis Set(集合)

学习 Redis Set(集合)

import java.util.Iterator;
import java.util.List;
import java.util.Set;

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 TestRedisSet {
	
	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();
		
		//SADD 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。
		long sAdd = rc.sAdd("man".getBytes(), 
				"a".getBytes(), 
				"b".getBytes(),
				"c".getBytes(), 
				"c".getBytes(), 
				"d".getBytes(), 
				"e".getBytes(),
				"f".getBytes());
		System.out.println("SADD [" + sAdd + "]");
		
		//SMEMBERS 返回集合 key 中的所有成员。
		Set<byte[]> sMembers = rc.sMembers("man".getBytes());
		Iterator<byte[]> itr = sMembers.iterator();
		while (itr.hasNext()) {
			System.out.println("SMEMBERS [" + new String(itr.next()) + "]");
		}
		
		//SCARD 返回集合 key 的基数(集合中元素的数量)。
		long sCard = rc.sCard("man".getBytes());
		System.out.println("SCARD [" + sCard + "]");
		
		//SDIFF
		Set<byte[]> sDiff = rc.sDiff("aset".getBytes(), "bset".getBytes());
		Iterator<byte[]> sDiffItr = sDiff.iterator();
		while (sDiffItr.hasNext()) {
			System.out.println("SDIFF [" + new String(sDiffItr.next()) + "]");
		}
		
		//SDIFFSTORE 这个命令的作用和 SDIFF 类似,但它将结果保存到 destination 集合,而不是简单地返回结果集。
		long sDiffStore = rc.sDiffStore(
				"cset".getBytes(), 
				"aset".getBytes(), 
				"bset".getBytes());
		System.out.println("SDIFFSTORE [" + sDiffStore + "]");
		
		//SINTER 返回一个集合的全部成员,该集合是所有给定集合的交集。
		Set<byte[]> sInter = rc.sInter("aset".getBytes(), "bset".getBytes());
		Iterator<byte[]> itrSInter = sInter.iterator();
		while (itrSInter.hasNext()) {
			System.out.println("SINTER [" + new String(itrSInter.next()) + "]");
		}
		
		//SINTERSTORE
		long sInterStore = rc.sInterStore(
				"dset".getBytes(), 
				"aset".getBytes(), 
				"bset".getBytes());
		System.out.println("SINTERSTORE [" + sInterStore + "]");
		
		//SISMEMBER 判断 member 元素是否集合 key 的成员。
		boolean sIsMember = rc.sIsMember("man".getBytes(), "a".getBytes());
		System.out.println("SISMEMBER [" + sIsMember + "]");
		
		//SMOVE 将 member 元素从 source 集合移动到 destination 集合。
		boolean sMove = rc.sMove("man".getBytes(), "men".getBytes(), "f".getBytes());
		System.out.println("SMOVE [" + sMove + "]");
		
		//SPOP 移除并返回集合中的一个随机元素。
		byte[] sPop = rc.sPop("man".getBytes());
		System.out.println("SPOP [" + new String(sPop) + "]");
		
		//SRANDMEMBER 如果命令执行时,只提供了 key 参数,那么返回集合中的一个随机元素。
		/**
		 	从 Redis 2.6 版本开始, SRANDMEMBER 命令接受可选的 count 参数:
			如果 count 为正数,且小于集合基数,那么命令返回一个包含 count 个元素的数组,
			数组中的元素各不相同。如果 count 大于等于集合基数,那么返回整个集合。
			如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。
		*/
		byte[] sRandMember = rc.sRandMember("man".getBytes());
		System.out.println("SRANDMEMBER [" + new String(sRandMember) + "]");
		
		List<byte[]> sRandMemberListP = rc.sRandMember("man".getBytes(), 2);
		for (byte[] value : sRandMemberListP) {
			System.out.println("sRandMemberListP [" + new String(value) + "]");
		}
		
		List<byte[]> sRandMemberListC = rc.sRandMember("man".getBytes(), -4);
		for (byte[] value : sRandMemberListC) {
			System.out.println("sRandMemberListC [" + new String(value) + "]");
		}
		
		//SREM 移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。
		long sRem = rc.sRem("man".getBytes(), "a".getBytes(), "b".getBytes());
		System.out.println("SREM [" + sRem + "]");
		
		//SUNION 返回一个集合的全部成员,该集合是所有给定集合的并集。
		Set<byte[]> sUnion = rc.sUnion("aset".getBytes(), "bset".getBytes());
		Iterator<byte[]> itrSUnion = sUnion.iterator();
		while (itrSUnion.hasNext()) {
			System.out.println("SUNION [" + new String(itrSUnion.next()) + "]");
		}
		
		//SUNIONSTORE 这个命令类似于 SUNION 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集。
		long sUnionStore = rc.sUnionStore("eset".getBytes(), "aset".getBytes(), "bset".getBytes());
		System.out.println("SUNIONSTORE [" + sUnionStore + "]");
		
		//SSCAN
		ScanOptionsBuilder sob = ScanOptions.scanOptions().count(1);
		ScanOptions options = sob.build();
		Cursor<byte[]>  sScan = rc.sScan("man".getBytes(), options);
		while (sScan.hasNext()) {
			System.out.println("SSCAN [" + new String(sScan.next()) + "]");
		}
		
	}

}

猜你喜欢

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