-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/config/redis/BaseWxMpRedisOps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package me.chanjar.weixin.mp.config.redis; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
/** | ||
* 微信公众号redis操作基本类 | ||
* <p> | ||
* 非内置实现redis相关操作, 请实现该类 | ||
*/ | ||
public class BaseWxMpRedisOps implements WxMpRedisOps { | ||
|
||
@Override | ||
public String getValue(String key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void setValue(String key, String value, int expire, TimeUnit timeUnit) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Long getExpire(String key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void expire(String key, int expire, TimeUnit timeUnit) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Lock getLock(String key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/config/redis/JedisWxMpRedisOps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package me.chanjar.weixin.mp.config.redis; | ||
|
||
import lombok.AllArgsConstructor; | ||
import me.chanjar.weixin.common.util.locks.JedisDistributedLock; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
/** | ||
* Jedis实现相关操作 | ||
*/ | ||
@AllArgsConstructor | ||
public class JedisWxMpRedisOps implements WxMpRedisOps { | ||
|
||
private JedisPool jedisPool; | ||
|
||
@Override | ||
public String getValue(String key) { | ||
try (Jedis jedis = this.jedisPool.getResource()) { | ||
return jedis.get(key); | ||
} | ||
} | ||
|
||
@Override | ||
public void setValue(String key, String value, int expire, TimeUnit timeUnit) { | ||
try (Jedis jedis = this.jedisPool.getResource()) { | ||
jedis.psetex(key, timeUnit.toMillis(expire), value); | ||
} | ||
} | ||
|
||
@Override | ||
public Long getExpire(String key) { | ||
try (Jedis jedis = this.jedisPool.getResource()) { | ||
return jedis.ttl(key); | ||
} | ||
} | ||
|
||
@Override | ||
public void expire(String key, int expire, TimeUnit timeUnit) { | ||
try (Jedis jedis = this.jedisPool.getResource()) { | ||
jedis.pexpire(key, timeUnit.toMillis(expire)); | ||
} | ||
} | ||
|
||
@Override | ||
public Lock getLock(String key) { | ||
return new JedisDistributedLock(jedisPool, key); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/config/redis/WxMpRedisOps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package me.chanjar.weixin.mp.config.redis; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
/** | ||
* 微信公众号Redis相关操作 | ||
* <p> | ||
* 该接口不承诺稳定, 外部实现请继承{@link BaseWxMpRedisOps} | ||
* | ||
* @see BaseWxMpRedisOps 实现需要继承该类 | ||
* @see JedisWxMpRedisOps jedis实现 | ||
*/ | ||
public interface WxMpRedisOps { | ||
|
||
String getValue(String key); | ||
|
||
void setValue(String key, String value, int expire, TimeUnit timeUnit); | ||
|
||
Long getExpire(String key); | ||
|
||
void expire(String key, int expire, TimeUnit timeUnit); | ||
|
||
Lock getLock(String key); | ||
} |