Java源码示例:org.redisson.api.RSetCache
示例1
@Test
public void testRemoveAll() {
RSetCache<String> s = redisson.getSetCache("test");
s.add("1");
s.add("3");
RTransaction t = redisson.createTransaction(TransactionOptions.defaults());
RSetCache<String> set = t.getSetCache("test");
Set<String> putSet = new HashSet<String>();
putSet.add("4");
putSet.add("3");
set.removeAll(putSet);
assertThat(s).containsOnly("1", "3");
assertThat(set).containsOnly("1");
t.commit();
assertThat(s).containsOnly("1");
}
示例2
@Test
public void testPutAll() {
RSetCache<String> s = redisson.getSetCache("test");
s.add("1");
s.add("3");
RTransaction t = redisson.createTransaction(TransactionOptions.defaults());
RSetCache<String> set = t.getSetCache("test");
Set<String> putSet = new HashSet<String>();
putSet.add("4");
putSet.add("6");
set.addAll(putSet);
assertThat(s).containsOnly("1", "3");
assertThat(set).containsOnly("1", "3", "4", "6");
t.commit();
assertThat(s).containsOnly("1", "3", "4", "6");
}
示例3
@Test
public void testAdd() {
RSetCache<String> s = redisson.getSetCache("test");
s.add("1");
s.add("3");
RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
RSetCache<String> set = transaction.getSetCache("test");
assertThat(set.add("4")).isTrue();
assertThat(set.add("3")).isFalse();
assertThat(set.contains("4")).isTrue();
assertThat(s.contains("4")).isFalse();
transaction.commit();
assertThat(s.size()).isEqualTo(3);
assertThat(s.contains("1")).isTrue();
assertThat(s.contains("3")).isTrue();
assertThat(s.contains("4")).isTrue();
}
示例4
@Test
public void testAddTTL() throws InterruptedException {
RSetCache<String> s = redisson.getSetCache("test");
s.add("1");
s.add("3");
RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
RSetCache<String> set = transaction.getSetCache("test");
assertThat(set.add("4", 2, TimeUnit.SECONDS)).isTrue();
assertThat(set.add("3")).isFalse();
assertThat(set.contains("4")).isTrue();
assertThat(s.contains("4")).isFalse();
transaction.commit();
assertThat(s.size()).isEqualTo(3);
assertThat(s.contains("1")).isTrue();
assertThat(s.contains("3")).isTrue();
assertThat(s.contains("4")).isTrue();
Thread.sleep(2000);
assertThat(s.contains("4")).isFalse();
}
示例5
@Test
public void testRemove() {
RSetCache<String> s = redisson.getSetCache("test");
s.add("1");
s.add("3");
RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
RSetCache<String> set = transaction.getSetCache("test");
assertThat(set.contains("1")).isTrue();
assertThat(set.remove("3")).isTrue();
assertThat(set.remove("3")).isFalse();
assertThat(set.remove("3")).isFalse();
assertThat(s.contains("3")).isTrue();
transaction.commit();
assertThat(s.size()).isEqualTo(1);
assertThat(s.contains("1")).isTrue();
assertThat(s.contains("3")).isFalse();
}
示例6
@Test
public void testAddExpireThenAdd() throws InterruptedException, ExecutionException {
RSetCache<String> set = redisson.getSetCache("simple31");
assertThat(set.add("123", 500, TimeUnit.MILLISECONDS)).isTrue();
Thread.sleep(500);
assertThat(set.size()).isEqualTo(1);
assertThat(set.contains("123")).isFalse();
assertThat(set.add("123")).isTrue();
Thread.sleep(1000);
assertThat(set.contains("123")).isTrue();
set.destroy();
}
示例7
@Test
public void testExpireOverwrite() throws InterruptedException, ExecutionException {
RSetCache<String> set = redisson.getSetCache("simple");
assertThat(set.add("123", 1, TimeUnit.SECONDS)).isTrue();
Thread.sleep(800);
assertThat(set.add("123", 1, TimeUnit.SECONDS)).isFalse();
Thread.sleep(100);
assertThat(set.contains("123")).isTrue();
Thread.sleep(300);
assertThat(set.contains("123")).isTrue();
set.destroy();
}
示例8
@Test
public void testRemove() throws InterruptedException, ExecutionException {
RSetCache<Integer> set = redisson.getSetCache("simple");
set.add(1, 1, TimeUnit.SECONDS);
set.add(3, 2, TimeUnit.SECONDS);
set.add(7, 3, TimeUnit.SECONDS);
Assert.assertTrue(set.remove(1));
Assert.assertFalse(set.contains(1));
assertThat(set).containsOnly(3, 7);
Assert.assertFalse(set.remove(1));
assertThat(set).containsOnly(3, 7);
Assert.assertTrue(set.remove(3));
Assert.assertFalse(set.contains(3));
assertThat(set).containsOnly(7);
Assert.assertEquals(1, set.size());
set.destroy();
}
示例9
@Test
public void testIteratorRemoveHighVolume() throws InterruptedException {
RSetCache<Integer> set = redisson.getSetCache("set");
for (int i = 1; i <= 5000; i++) {
set.add(i);
set.add(i*100000, 20, TimeUnit.SECONDS);
}
int cnt = 0;
Iterator<Integer> iterator = set.iterator();
while (iterator.hasNext()) {
Integer integer = iterator.next();
iterator.remove();
cnt++;
}
Assert.assertEquals(10000, cnt);
Assert.assertEquals(0, set.size());
set.destroy();
}
示例10
@Test
public void testToArray() throws InterruptedException {
RSetCache<String> set = redisson.getSetCache("set");
set.add("1");
set.add("4");
set.add("2", 1, TimeUnit.SECONDS);
set.add("5");
set.add("3");
Thread.sleep(1500);
assertThat(set.toArray()).containsOnly("1", "4", "5", "3");
String[] strs = set.toArray(new String[0]);
assertThat(strs).containsOnly("1", "4", "5", "3");
set.destroy();
}
示例11
@Test
public void testContains() throws InterruptedException {
RSetCache<TestObject> set = redisson.getSetCache("set");
set.add(new TestObject("1", "2"));
set.add(new TestObject("1", "2"));
set.add(new TestObject("2", "3"), 1, TimeUnit.SECONDS);
set.add(new TestObject("3", "4"));
set.add(new TestObject("5", "6"));
Thread.sleep(1000);
Assert.assertFalse(set.contains(new TestObject("2", "3")));
Assert.assertTrue(set.contains(new TestObject("1", "2")));
Assert.assertFalse(set.contains(new TestObject("1", "9")));
set.destroy();
}
示例12
public static void main(String[] args) {
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
RSetCache<String> setCache = redisson.getSetCache("mySet");
// with ttl = 20 seconds
boolean isAdded = setCache.add("1", 20, TimeUnit.SECONDS);
// store value permanently
setCache.add("2");
setCache.contains("1");
for (String string : setCache) {
// iteration through bulk loaded values
}
boolean removedValue = setCache.remove("1");
setCache.removeAll(Arrays.asList("1", "2", "3"));
setCache.containsAll(Arrays.asList("4", "1", "0"));
RSet<String> secondsSet = redisson.getSet("mySecondsSet");
secondsSet.add("4");
secondsSet.add("5");
Set<String> allValues = secondsSet.readAll();
redisson.shutdown();
}
示例13
@Override
public void commit(CommandAsyncExecutor commandExecutor) {
RSetCache<Object> set = new RedissonSetCache<>(codec, null, commandExecutor, name, null);
if (timeUnit != null) {
set.addAsync(value, ttl, timeUnit);
} else {
set.addAsync(value);
}
getLock(set, commandExecutor, value).unlockAsync(threadId);
}
示例14
@Test
public void testKeySet() {
RSetCache<String> s = redisson.getSetCache("test");
s.add("1");
s.add("3");
RTransaction t = redisson.createTransaction(TransactionOptions.defaults());
RSetCache<String> set = t.getSetCache("test");
set.remove("3");
assertThat(set).containsOnly("1");
assertThat(s).containsOnly("1", "3");
}
示例15
@Test
public void testDestroy() {
RSetCache<String> cache = redisson.getSetCache("test");
EvictionScheduler evictionScheduler = ((Redisson)redisson).getEvictionScheduler();
Map<?, ?> map = Reflect.on(evictionScheduler).get("tasks");
assertThat(map.isEmpty()).isFalse();
cache.destroy();
assertThat(map.isEmpty()).isTrue();
}
示例16
@Test
public void testRemoveAll() {
RSetCache<Integer> set = redisson.getSetCache("set");
set.add(1);
set.add(2, 10, TimeUnit.SECONDS);
set.add(3);
assertThat(set.removeAll(Arrays.asList(1, 3))).isTrue();
assertThat(set.removeAll(Arrays.asList(1, 3))).isFalse();
assertThat(set).containsOnly(2);
set.destroy();
}
示例17
@Test
public void testDelete() {
RSetCache<Integer> set = redisson.getSetCache("set");
assertThat(set.delete()).isFalse();
set.add(1, 1, TimeUnit.SECONDS);
assertThat(set.delete()).isTrue();
assertThat(set.delete()).isFalse();
set.destroy();
}
示例18
@Test
public void testAddBigBean() {
RSetCache<Map<Integer, Integer>> set = redisson.getSetCache("simple");
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < 150; i++) {
map.put(i, i);
}
set.add(map);
map.remove(0);
set.add(map);
set.iterator().next();
set.destroy();
}
示例19
@Test
public void testAddBean() throws InterruptedException, ExecutionException {
SimpleBean sb = new SimpleBean();
sb.setLng(1L);
RSetCache<SimpleBean> set = redisson.getSetCache("simple");
assertThat(set.add(sb)).isTrue();
Assert.assertEquals(sb.getLng(), set.iterator().next().getLng());
set.destroy();
}
示例20
@Test
public void testAddExpire() throws InterruptedException, ExecutionException {
RSetCache<String> set = redisson.getSetCache("simple3");
assertThat(set.add("123", 500, TimeUnit.MILLISECONDS)).isTrue();
assertThat(set).contains("123");
Thread.sleep(500);
assertThat(set.size()).isEqualTo(1);
assertThat(set).doesNotContain("123");
assertThat(set.add("123", 1, TimeUnit.SECONDS)).isTrue();
set.destroy();
}
示例21
@Test
public void testAddOverrideExpiration() throws InterruptedException {
RSetCache<String> set = redisson.getSetCache("simple31");
assertThat(set.add("123", 500, TimeUnit.MILLISECONDS)).isTrue();
Thread.sleep(400);
assertThat(set.add("123", 3, TimeUnit.SECONDS)).isFalse();
Thread.sleep(2000);
assertThat(set.contains("123")).isTrue();
set.destroy();
}
示例22
@Test
public void testAddExpireTwise() throws InterruptedException, ExecutionException {
RSetCache<String> set = redisson.getSetCache("simple31");
assertThat(set.add("123", 1, TimeUnit.SECONDS)).isTrue();
Thread.sleep(1000);
Assert.assertFalse(set.contains("123"));
assertThat(set.add("4341", 1, TimeUnit.SECONDS)).isTrue();
Thread.sleep(1000);
Assert.assertFalse(set.contains("4341"));
set.destroy();
}
示例23
@Test
public void testIteratorRemove() throws InterruptedException {
RSetCache<String> set = redisson.getSetCache("list");
set.add("1");
set.add("4", 1, TimeUnit.SECONDS);
set.add("2");
set.add("5", 1, TimeUnit.SECONDS);
set.add("3");
Thread.sleep(1000);
for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
String value = iterator.next();
if (value.equals("2")) {
iterator.remove();
}
}
assertThat(set).contains("1", "3");
int iteration = 0;
for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
iterator.next();
iterator.remove();
iteration++;
}
Assert.assertEquals(2, iteration);
Assert.assertFalse(set.contains("4"));
Assert.assertFalse(set.contains("5"));
set.destroy();
}
示例24
@Test
public void testIteratorSequence() {
RSetCache<Long> set = redisson.getSetCache("set");
for (int i = 0; i < 1000; i++) {
set.add(Long.valueOf(i));
}
Set<Long> setCopy = new HashSet<Long>();
for (int i = 0; i < 1000; i++) {
setCopy.add(Long.valueOf(i));
}
checkIterator(set, setCopy);
set.destroy();
}
示例25
@Test
public void testRetainAll() throws InterruptedException {
RSetCache<Integer> set = redisson.getSetCache("set");
for (int i = 0; i < 10000; i++) {
set.add(i);
set.add(i*10, 15, TimeUnit.SECONDS);
}
Assert.assertTrue(set.retainAll(Arrays.asList(1, 2)));
Thread.sleep(500);
assertThat(set).containsOnly(1, 2);
Assert.assertEquals(2, set.size());
set.destroy();
}
示例26
@Test
public void testContainsAll() {
RSetCache<Integer> set = redisson.getSetCache("set");
for (int i = 0; i < 200; i++) {
set.add(i);
}
Assert.assertTrue(set.containsAll(Collections.emptyList()));
Assert.assertTrue(set.containsAll(Arrays.asList(30, 11)));
Assert.assertFalse(set.containsAll(Arrays.asList(30, 711, 11)));
set.destroy();
}
示例27
@Test
public void testDuplicates() {
RSetCache<TestObject> set = redisson.getSetCache("set");
set.add(new TestObject("1", "2"));
set.add(new TestObject("1", "2"));
set.add(new TestObject("2", "3"));
set.add(new TestObject("3", "4"));
set.add(new TestObject("5", "6"));
Assert.assertEquals(4, set.size());
set.destroy();
}
示例28
@Test
public void testSize() {
RSetCache<Integer> set = redisson.getSetCache("set");
set.add(1);
set.add(2);
set.add(3);
set.add(3);
set.add(4);
set.add(5);
set.add(5);
Assert.assertEquals(5, set.size());
set.destroy();
}
示例29
@Test
public void testReadAllExpired() throws InterruptedException {
RSetCache<Integer> set = redisson.getSetCache("set");
set.add(1, 1, TimeUnit.SECONDS);
Thread.sleep(1005);
assertThat(set.readAll()).isEmpty();
set.destroy();
}
示例30
@Test
public void testReadAll() {
RSetCache<Integer> set = redisson.getSetCache("set");
set.add(1, 2, TimeUnit.MINUTES);
set.add(2);
set.add(3);
set.add(4);
set.add(5);
assertThat(set.readAll()).containsOnly(1, 2, 3, 4, 5);
set.destroy();
}