使用注解后支持pipelined,watch,unwatch,multi方法
24 | * Created on 2014/9/26. 25 | * 26 | * @author FZY 27 | */ 28 | @Target({ElementType.METHOD}) 29 | @Retention(RetentionPolicy.RUNTIME) 30 | @Inherited 31 | @Documented 32 | public @interface Redis { 33 | 34 | String value() default ""; 35 | 36 | boolean shard() default false; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/fangzy/redis/RedisAdvisor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014-2015 ,fangzy (zyuanf@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.fangzy.redis; 18 | 19 | import org.aopalliance.aop.Advice; 20 | import org.springframework.aop.Pointcut; 21 | import org.springframework.aop.support.AbstractPointcutAdvisor; 22 | import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.stereotype.Component; 25 | 26 | /** 27 | * Created on 2014/9/26. 28 | * 29 | * @author FZY 30 | */ 31 | @Component 32 | public class RedisAdvisor extends AbstractPointcutAdvisor { 33 | 34 | private Pointcut pointcut = new AnnotationMatchingPointcut(null, Redis.class); 35 | 36 | @Autowired 37 | private RedisInterceptor redisInterceptor; 38 | 39 | @Override 40 | public Pointcut getPointcut() { 41 | return pointcut; 42 | } 43 | 44 | @Override 45 | public Advice getAdvice() { 46 | return redisInterceptor; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/fangzy/redis/RedisInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014-2015 ,fangzy (zyuanf@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.fangzy.redis; 18 | 19 | import org.aopalliance.intercept.MethodInterceptor; 20 | import org.aopalliance.intercept.MethodInvocation; 21 | import org.springframework.stereotype.Component; 22 | 23 | /** 24 | * Created on 2014/9/27. 25 | * 26 | * @author FZY 27 | */ 28 | @Component 29 | public class RedisInterceptor implements MethodInterceptor { 30 | 31 | private JedisHolder jedisHolder; 32 | 33 | @Override 34 | public Object invoke(MethodInvocation invocation) throws Throwable { 35 | Redis redis = invocation.getMethod().getDeclaredAnnotation(Redis.class); 36 | if (redis == null) { 37 | throw new IllegalArgumentException("Can not find @Redis annotation."); 38 | } 39 | if (redis.shard()) { 40 | return shardedRedis(invocation); 41 | } else { 42 | return normalRedis(invocation, redis); 43 | } 44 | } 45 | 46 | private Object shardedRedis(MethodInvocation invocation) throws Throwable { 47 | try { 48 | jedisHolder.createShardedResource(); 49 | return invocation.proceed(); 50 | } finally { 51 | jedisHolder.releaseShardedForce(); 52 | } 53 | } 54 | 55 | private Object normalRedis(MethodInvocation invocation, Redis redis) throws Throwable { 56 | String val = redis.value(); 57 | try { 58 | jedisHolder.createResource(val); 59 | return invocation.proceed(); 60 | } finally { 61 | jedisHolder.releaseForce(); 62 | } 63 | } 64 | 65 | public void setJedisHolder(JedisHolder jedisHolder) { 66 | this.jedisHolder = jedisHolder; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/github/fangzy/redis/jedis/JedisCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014-2015 ,fangzy (zyuanf@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.fangzy.redis.jedis; 18 | 19 | import com.github.fangzy.redis.AbstractCallback; 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | import org.springframework.cglib.proxy.MethodProxy; 23 | import org.springframework.stereotype.Component; 24 | import redis.clients.jedis.Jedis; 25 | 26 | import java.lang.reflect.Method; 27 | 28 | /** 29 | * Jedis拦截器 30 | * Created by fzy on 2014/7/6. 31 | */ 32 | @Component 33 | public class JedisCallback extends AbstractCallback { 34 | 35 | private static final Logger LOGGER = LoggerFactory.getLogger(JedisCallback.class); 36 | 37 | @Override 38 | public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { 39 | Jedis jedis = null; 40 | filterMethod(method); 41 | try { 42 | jedis = jedisHolder.get(); 43 | return methodProxy.invoke(jedis, objects); 44 | } catch (Exception e) { 45 | LOGGER.error(e.getMessage()); 46 | throw e; 47 | } finally { 48 | release(jedis); 49 | } 50 | } 51 | 52 | private void release(Jedis jedis) { 53 | if (jedis == null) { 54 | return; 55 | } 56 | jedisHolder.release(jedis); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/fangzy/redis/jedis/JedisFactoryBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014-2015 ,fangzy (zyuanf@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.fangzy.redis.jedis; 18 | 19 | import com.github.fangzy.redis.JedisCallbackFilter; 20 | import org.springframework.beans.factory.FactoryBean; 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.cglib.proxy.Callback; 23 | import org.springframework.cglib.proxy.CallbackFilter; 24 | import org.springframework.cglib.proxy.Enhancer; 25 | import org.springframework.cglib.proxy.NoOp; 26 | import org.springframework.stereotype.Component; 27 | import redis.clients.jedis.Jedis; 28 | 29 | /** 30 | * Jedis代理 31 | * Created by fzy on 2014/7/6. 32 | */ 33 | @Component 34 | public class JedisFactoryBean implements FactoryBean