# spring-redis-plugin
**Repository Path**: 5vbear/spring-redis-plugin
## Basic Information
- **Project Name**: spring-redis-plugin
- **Description**: jedis plugin for spring
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2016-04-14
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
spring-redis-plugin [](https://travis-ci.org/fangzy/spring-redis-plugin)
===========
本项目是[Jedis](https://github.com/xetorthio/jedis)基于spring的增强版,实现了自动获取连接和关闭连接,提供基于redis的分布式锁实现.
## 1 Maven POM 配置
暂时使用Github做为Maven仓库
### 1.1 添加仓库地址
```xml
spring-redis-plugin-releases
https://raw.github.com/fangzy/spring-redis-plugin/master/releases
```
### 1.2 添加依赖
```xml
org.reindeer
spring-redis-plugin
1.3
```
### 1.3 依赖`spring-context`项目
```xml
org.springframework
spring-context
4.X.X.RELEASE
commons-logging
commons-logging
```
## 2 使用方法
### 2.1 spring配置文件
```xml
```
> 请按照需要选择配置
### 2.2 依赖注入
```java
@Service
public class JedisDemo {
@Autowired
private Jedis jedis;
public void setAndGet(String key,int times) {
for (int i=0;i 如果需要使用pipelined,watch,unwatch,multi 方法必须开启注解,详见2.3
### 2.3 使用注解
```java
@Service
public class JedisDemo {
@Autowired
private Jedis jedis;
@Redis
public void incr(String key,int times) {
Pipeline pipeline = jedis.pipelined();
pipeline.set(key, "1");
for (int i = 0; i < times; i++) {
pipeline.incr(key);
}
Response response = pipeline.get(key);
pipeline.sync();
jedis.del(key);
}
}
```
### 2.4 使用JedisProxy
```java
public class JedisDemo {
public static void setAndGet(String key,int times) {
Jedis jedis = JedisProxy.create();
for (int i=0;i JedisProxy 通常用于静态方法,但是需要在spring容器全部加载完毕后使用
如果需要使用pipelined,watch,unwatch,multi 方法必须开启注解,详见2.3
### 2.5 使用多数据源
```java
@Service
public class JedisDemo {
@Autowired
private Jedis jedis;
@Redis("anotherRedis")
public void incr(String key,int times) {
Pipeline pipeline = jedis.pipelined();
pipeline.set(key, "1");
for (int i = 0; i < times; i++) {
pipeline.incr(key);
}
Response response = pipeline.get(key);
pipeline.sync();
jedis.del(key);
}
}
```
```xml
```
> jedisPoolMap中的default为默认数据源
### 2.6 使用shard
```java
@Service
public class ShardedJedisDemo {
@Autowired
private ShardedJedis shardedJedis;
@Redis(shard = true)
public void incr(String key, int times) {
ShardedJedisPipeline pipeline = shardedJedis.pipelined();
for (int i = 0; i < times; i++) {
pipeline.incr(key + i);
}
Response response = pipeline.get(key + 1);
pipeline.sync();
LOGGER.info(response.get());
}
}
```
```xml
```