diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/HashOperationsEditor.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/HashOperationsEditor.java new file mode 100644 index 000000000..2cb2b509d --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/HashOperationsEditor.java @@ -0,0 +1,38 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.core; + +import java.beans.PropertyEditorSupport; + +/** + * PropertyEditor allowing for easy injection of {@link HashOperations} from + * {@link RedisOperations}. + * + * @author Costin Leau + */ +class HashOperationsEditor extends PropertyEditorSupport { + + @Override + public void setValue(Object value) { + if (value instanceof RedisOperations) { + super.setValue(((RedisOperations) value).opsForHash()); + } + else { + throw new java.lang.IllegalArgumentException("Editor supports only conversion of type " + + RedisOperations.class); + } + } +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ListOperationsEditor.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ListOperationsEditor.java new file mode 100644 index 000000000..9b1ebb663 --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ListOperationsEditor.java @@ -0,0 +1,37 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.core; + +import java.beans.PropertyEditorSupport; + +/** + * PropertyEditor allowing for easy injection of {@link ListOperations} from + * {@link RedisOperations}. + * + * @author Costin Leau + */ +class ListOperationsEditor extends PropertyEditorSupport { + @Override + public void setValue(Object value) { + if (value instanceof RedisOperations) { + super.setValue(((RedisOperations) value).opsForList()); + } + else { + throw new java.lang.IllegalArgumentException("Editor supports only conversion of type " + + RedisOperations.class); + } + } +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/SetOperationsEditor.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/SetOperationsEditor.java new file mode 100644 index 000000000..2cc6704af --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/SetOperationsEditor.java @@ -0,0 +1,38 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.core; + +import java.beans.PropertyEditorSupport; + +/** + * PropertyEditor allowing for easy injection of {@link SetOperations} from + * {@link RedisOperations}. + * + * @author Costin Leau + */ +class SetOperationsEditor extends PropertyEditorSupport { + + @Override + public void setValue(Object value) { + if (value instanceof RedisOperations) { + super.setValue(((RedisOperations) value).opsForSet()); + } + else { + throw new java.lang.IllegalArgumentException("Editor supports only conversion of type " + + RedisOperations.class); + } + } +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/StringRedisConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/StringRedisConnection.java new file mode 100644 index 000000000..0662d8a35 --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/StringRedisConnection.java @@ -0,0 +1,238 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.core; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.springframework.data.keyvalue.redis.connection.DataType; +import org.springframework.data.keyvalue.redis.connection.MessageListener; +import org.springframework.data.keyvalue.redis.connection.RedisConnection; +import org.springframework.data.keyvalue.redis.connection.SortParameters; +import org.springframework.data.keyvalue.redis.serializer.RedisSerializer; + +/** + * Convenience extension of {@link RedisConnection} that accepts and returns {@link String}s instead of + * byte arrays. Uses a {@link RedisSerializer} underneath to perform the conversion. + * + * @author Costin Leau + * @see RedisCallback + * @see RedisSerializer + * @see StringRedisTemplate + */ +public interface StringRedisConnection extends RedisConnection { + + Boolean exists(String key); + + Long del(String... keys); + + DataType type(String key); + + Collection keys(String pattern); + + void rename(String oldName, String newName); + + Boolean renameNX(String oldName, String newName); + + Boolean expire(String key, long seconds); + + Boolean expireAt(String key, long unixTime); + + Boolean persist(String key); + + Long ttl(String key); + + String echo(String message); + + // sort commands + List sort(String key, SortParameters params); + + Long sort(String key, SortParameters params, String storeKey); + + String get(String key); + + String getSet(String key, String value); + + List mGet(String... keys); + + void set(String key, String value); + + Boolean setNX(String key, String value); + + void setEx(String key, long seconds, String value); + + void mSetString(Map tuple); + + void mSetNXString(Map tuple); + + Long incr(String key); + + Long incrBy(String key, long value); + + Long decr(String key); + + Long decrBy(String key, long value); + + Long append(String key, String value); + + String getRange(String key, int start, int end); + + void setRange(String key, int start, int end); + + Boolean getBit(String key, long offset); + + void setBit(String key, long offset, boolean value); + + Long strLen(String key); + + Long rPush(String key, String value); + + Long lPush(String key, String value); + + Long rPushX(String key, String value); + + Long lPushX(String key, String value); + + Long lLen(String key); + + List lRange(String key, long start, long end); + + void lTrim(String key, long start, long end); + + String lIndex(String key, long index); + + Long lInsert(String key, POSITION where, String pivot, String value); + + void lSet(String key, long index, String value); + + Long lRem(String key, long count, String value); + + String lPop(String key); + + String rPop(String key); + + List bLPop(int timeout, String... keys); + + List bRPop(int timeout, String... keys); + + String rPopLPush(String srcKey, String dstKey); + + String bRPopLPush(int timeout, String srcKey, String dstKey); + + Boolean sAdd(String key, String value); + + Boolean sRem(String key, String value); + + String sPop(String key); + + Boolean sMove(String srcKey, String destKey, String value); + + Long sCard(String key); + + Boolean sIsMember(String key, String value); + + Set sInter(String... keys); + + void sInterStore(String destKey, String... keys); + + Set sUnion(String... keys); + + void sUnionStore(String destKey, String... keys); + + Set sDiff(String... keys); + + void sDiffStore(String destKey, String... keys); + + Set sMembers(String key); + + String sRandMember(String key); + + Boolean zAdd(String key, double score, String value); + + Boolean zRem(String key, String value); + + Double zIncrBy(String key, double increment, String value); + + Long zRank(String key, String value); + + Long zRevRank(String key, String value); + + Set zRange(String key, long start, long end); + + Set zRangeWithScore(String key, long start, long end); + + Set zRevRange(String key, long start, long end); + + Set zRevRangeWithScore(String key, long start, long end); + + Set zRangeByScore(String key, double min, double max); + + Set zRangeByScoreWithScore(String key, double min, double max); + + Set zRangeByScore(String key, double min, double max, long offset, long count); + + Set zRangeByScoreWithScore(String key, double min, double max, long offset, long count); + + Long zCount(String key, double min, double max); + + Long zCard(String key); + + Double zScore(String key, String value); + + Long zRemRange(String key, long start, long end); + + Long zRemRangeByScore(String key, double min, double max); + + Long zUnionStore(String destKey, String... sets); + + Long zUnionStore(String destKey, Aggregate aggregate, int[] weights, String... sets); + + Long zInterStore(String destKey, String... sets); + + Long zInterStore(String destKey, Aggregate aggregate, int[] weights, String... sets); + + Boolean hSet(String key, String field, String value); + + Boolean hSetNX(String key, String field, String value); + + String hGet(String key, String field); + + List hMGet(String key, String... fields); + + void hMSet(String key, Map hashes); + + Long hIncrBy(String key, String field, long delta); + + Boolean hExists(String key, String field); + + Boolean hDel(String key, String field); + + Long hLen(String key); + + Set hKeys(String key); + + List hVals(String key); + + Map hGetAll(String key); + + Long publish(String channel, String message); + + void subscribe(MessageListener listener, String... channels); + + void pSubscribe(MessageListener listener, String... patterns); +} \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ValueOperationsEditor.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ValueOperationsEditor.java new file mode 100644 index 000000000..325683483 --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ValueOperationsEditor.java @@ -0,0 +1,38 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.core; + +import java.beans.PropertyEditorSupport; + +/** + * PropertyEditor allowing for easy injection of {@link ValueOperations} from + * {@link RedisOperations}. + * + * @author Costin Leau + */ +class ValueOperationsEditor extends PropertyEditorSupport { + + @Override + public void setValue(Object value) { + if (value instanceof RedisOperations) { + super.setValue(((RedisOperations) value).opsForValue()); + } + else { + throw new java.lang.IllegalArgumentException("Editor supports only conversion of type " + + RedisOperations.class); + } + } +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ZSetOperationsEditor.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ZSetOperationsEditor.java new file mode 100644 index 000000000..902ba495e --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ZSetOperationsEditor.java @@ -0,0 +1,38 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.core; + +import java.beans.PropertyEditorSupport; + +/** + * PropertyEditor allowing for easy injection of {@link ZSetOperations} from + * {@link RedisOperations}. + * + * @author Costin Leau + */ +class ZSetOperationsEditor extends PropertyEditorSupport { + + @Override + public void setValue(Object value) { + if (value instanceof RedisOperations) { + super.setValue(((RedisOperations) value).opsForZSet()); + } + else { + throw new java.lang.IllegalArgumentException("Editor supports only conversion of type " + + RedisOperations.class); + } + } +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/PropertyEditorsTest.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/PropertyEditorsTest.java new file mode 100644 index 000000000..5c886e8e4 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/PropertyEditorsTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.GenericXmlApplicationContext; +import org.springframework.data.keyvalue.redis.core.RedisOperations; + +/** + * @author Costin Leau + */ +public class PropertyEditorsTest { + + private GenericXmlApplicationContext ctx; + + @Before + public void setUp() { + ctx = new GenericXmlApplicationContext("/org/springframework/data/keyvalue/redis/pe.xml"); + } + + @After + public void tearDown() { + if (ctx != null) + ctx.destroy(); + } + + @Test + public void testInjection() throws Exception { + RedisViewPE bean = ctx.getBean(RedisViewPE.class); + RedisOperations ops = ctx.getBean(RedisOperations.class); + + assertSame(ops.opsForValue(), bean.getValueOps()); + assertSame(ops.opsForList(), bean.getListOps()); + assertSame(ops.opsForSet(), bean.getSetOps()); + assertSame(ops.opsForZSet(), bean.getZsetOps()); + assertSame(ops, bean.getHashOps().getOperations()); + } +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/RedisViewPE.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/RedisViewPE.java new file mode 100644 index 000000000..3282ef2e3 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/RedisViewPE.java @@ -0,0 +1,74 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis; + +import org.springframework.data.keyvalue.redis.core.HashOperations; +import org.springframework.data.keyvalue.redis.core.ListOperations; +import org.springframework.data.keyvalue.redis.core.SetOperations; +import org.springframework.data.keyvalue.redis.core.ValueOperations; +import org.springframework.data.keyvalue.redis.core.ZSetOperations; + +/** + * @author Costin Leau + */ +public class RedisViewPE { + + private ValueOperations valueOps; + private ListOperations listOps; + private SetOperations setOps; + private ZSetOperations zsetOps; + private HashOperations hashOps; + + public ValueOperations getValueOps() { + return valueOps; + } + + public void setValueOps(ValueOperations valueOps) { + this.valueOps = valueOps; + } + + public ListOperations getListOps() { + return listOps; + } + + public void setListOps(ListOperations listOps) { + this.listOps = listOps; + } + + public SetOperations getSetOps() { + return setOps; + } + + public void setSetOps(SetOperations setOps) { + this.setOps = setOps; + } + + public ZSetOperations getZsetOps() { + return zsetOps; + } + + public void setZsetOps(ZSetOperations zsetOps) { + this.zsetOps = zsetOps; + } + + public HashOperations getHashOps() { + return hashOps; + } + + public void setHashOps(HashOperations hashOps) { + this.hashOps = hashOps; + } +} \ No newline at end of file diff --git a/spring-data-redis/src/test/resources/org/springframework/data/keyvalue/redis/pe.xml b/spring-data-redis/src/test/resources/org/springframework/data/keyvalue/redis/pe.xml new file mode 100644 index 000000000..50680e0a8 --- /dev/null +++ b/spring-data-redis/src/test/resources/org/springframework/data/keyvalue/redis/pe.xml @@ -0,0 +1,18 @@ + + + + + + + + +