From f802e6757e6684ec542834cd0d9fc887053391a9 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 4 Nov 2010 17:27:07 +0200 Subject: [PATCH] + initial skeleton for RedisTemplate & Co. --- .../datastore/redis/core/MyRedisAccessor.java | 55 ++++++++++++++++ .../datastore/redis/core/MyRedisCallback.java | 37 +++++++++++ .../redis/core/MyRedisOperations.java | 26 ++++++++ .../datastore/redis/core/MyRedisTemplate.java | 62 +++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisAccessor.java create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisCallback.java create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisOperations.java create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisTemplate.java diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisAccessor.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisAccessor.java new file mode 100644 index 000000000..09a270d7e --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisAccessor.java @@ -0,0 +1,55 @@ +/* + * Copyright 2006-2009 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.datastore.redis.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.datastore.redis.core.connection.RedisConnectionFactory; +import org.springframework.util.Assert; + +/** + * @author Costin Leau + */ +public class MyRedisAccessor implements InitializingBean { + + /** Logger available to subclasses */ + protected final Log logger = LogFactory.getLog(getClass()); + + private RedisConnectionFactory connectionFactory; + + public void afterPropertiesSet() { + Assert.notNull(getConnectionFactory(), "RedisConnectionFactory is required"); + } + + /** + * Returns the connectionFactory. + * + * @return Returns the connectionFactory + */ + public RedisConnectionFactory getConnectionFactory() { + return connectionFactory; + } + + /** + * Sets the connection factory. + * + * @param connectionFactory The connectionFactory to set. + */ + public void setConnectionFactory(RedisConnectionFactory connectionFactory) { + this.connectionFactory = connectionFactory; + } +} diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisCallback.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisCallback.java new file mode 100644 index 000000000..89f8d5b10 --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisCallback.java @@ -0,0 +1,37 @@ +/* + * Copyright 2006-2009 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.datastore.redis.core; + +import org.springframework.datastore.redis.core.connection.RedisConnection; + +/** + * Callback interface for Redis code. To be used with {@link MyRedisTemplate} execution methods, often as anonymous + * classes within a method implementation. + * + * @author Costin Leau + */ +public interface MyRedisCallback { + + /** + * Gets called by {@link MyRedisTemplate} with an active Redis connection. Does not need to care about activating or + * closing the connection or handling exceptions or transactions. + * + * @param connection + * @return + * @throws Exception + */ + T doInRedis(RedisConnection connection) throws Exception; +} diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisOperations.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisOperations.java new file mode 100644 index 000000000..ba4d6aa5c --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisOperations.java @@ -0,0 +1,26 @@ +/* + * Copyright 2006-2009 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.datastore.redis.core; + +/** + * Basic set of Redis operations, implemented by {@link MyRedisTemplate}. + * + * @author Costin Leau + */ +public interface MyRedisOperations { + + +} diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisTemplate.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisTemplate.java new file mode 100644 index 000000000..db94d56fb --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/MyRedisTemplate.java @@ -0,0 +1,62 @@ +/* + * Copyright 2006-2009 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.datastore.redis.core; + +import org.springframework.datastore.redis.core.connection.RedisConnection; +import org.springframework.datastore.redis.core.connection.RedisConnectionFactory; + +/** + * + * Helper class that simplifies Redis data access code. Automatically converts Redis client exceptions into + * DataAccessExceptions, following the org.springframework.dao exception hierarchy. + * + * The central method is execute, supporting Redis access code implementing the {@link MyRedisCallback} interface. + * It provides {@link RedisConnection} handling such that neither the {@link MyRedisCallback} implementation nor + * the calling code needs to explicitly care about retrieving/closing Redis connections, or handling Session + * lifecycle exceptions. For typical single step actions, there are various convenience methods. + * + * This is the central class in Redis support. + * Simplifies the use of Redis and helps avoid common errors. + * + * @author Costin Leau + */ +public class MyRedisTemplate extends MyRedisAccessor { + + private boolean exposeConnection = false; + + public MyRedisTemplate() { + } + + public MyRedisTemplate(RedisConnectionFactory connectionFactory) { + this.setConnectionFactory(connectionFactory); + afterPropertiesSet(); + } + + public T execute(MyRedisCallback action) { + throw new UnsupportedOperationException(); + } + + /** + * Sets whether to expose the Redis connection to {@link MyRedisCallback} code. + * + * Default is "false": a proxy will be returned, suppressing quit and disconnect calls. + * + * @param exposeConnection + */ + public void setExposeConnection(boolean exposeConnection) { + this.exposeConnection = exposeConnection; + } +}