码迷,mamicode.com
首页 > 其他好文 > 详细

DefaultHashOperations multiget的一个坑

时间:2016-10-31 22:04:19      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:value   zab   callback   private   rri   values   support   return   int   

 

DefaultHashOperations的multiget如果没有数据会返回java.util.Collections.EmptyList,这个List没有重写add方法。

 

List<Long> resultIds=redisTemplate.boundHashOps(“key”).multiGet(ids)

 

resultIds在add一个新元素时,报了一下的错:
java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:148)
        at java.util.AbstractList.add(AbstractList.java:108)

 

原因:

org.springframework.data.redis.core.RedisTemplate#boundHashOps

    public List<HV> multiGet(Collection<HK> hashKeys) {
        return ops.multiGet(getKey(), hashKeys);
    }

org.springframework.data.redis.core.DefaultHashOperations#multiGet

    public List<HV> multiGet(K key, Collection<HK> fields) {
        if (fields.isEmpty()) {
            return Collections.emptyList();//这个list是java.util.Collections#emptyList中的java.util.Collections.EmptyList
        }

        final byte[] rawKey = rawKey(key);

        final byte[][] rawHashKeys = new byte[fields.size()][];

        int counter = 0;
        for (HK hashKey : fields) {
            rawHashKeys[counter++] = rawHashKey(hashKey);
        }

        List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {

            public List<byte[]> doInRedis(RedisConnection connection) {
                return connection.hMGet(rawKey, rawHashKeys);
            }
        }, true);

        return deserializeHashValues(rawValues);
    }

 

这个java.util.Collections.EmptyList没有重写add方法:

    private static class EmptyList<E>
        extends AbstractList<E>
        implements RandomAccess, Serializable {
        private static final long serialVersionUID = 8842843931221139166L;

        public Iterator<E> iterator() {
            return emptyIterator();
        }
        public ListIterator<E> listIterator() {
            return emptyListIterator();
        }

        public int size() {return 0;}
        public boolean isEmpty() {return true;}

        public boolean contains(Object obj) {return false;}
        public boolean containsAll(Collection<?> c) { return c.isEmpty(); }

        public Object[] toArray() { return new Object[0]; }

        public <T> T[] toArray(T[] a) {
            if (a.length > 0)
                a[0] = null;
            return a;
        }

        public E get(int index) {
            throw new IndexOutOfBoundsException("Index: "+index);
        }

        public boolean equals(Object o) {
            return (o instanceof List) && ((List<?>)o).isEmpty();
        }

        public int hashCode() { return 1; }

        @Override
        public boolean removeIf(Predicate<? super E> filter) {
            Objects.requireNonNull(filter);
            return false;
        }
        @Override
        public void replaceAll(UnaryOperator<E> operator) {
            Objects.requireNonNull(operator);
        }
        @Override
        public void sort(Comparator<? super E> c) {
        }

        // Override default methods in Collection
        @Override
        public void forEach(Consumer<? super E> action) {
            Objects.requireNonNull(action);
        }

        @Override
        public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }

        // Preserves singleton property
        private Object readResolve() {
            return EMPTY_LIST;
        }
    }

 

DefaultHashOperations multiget的一个坑

标签:value   zab   callback   private   rri   values   support   return   int   

原文地址:http://www.cnblogs.com/softidea/p/6017142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!