Skip to content

Commit

Permalink
Add RedisClient and support expire ttl ... command
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Jul 20, 2021
1 parent b5a855f commit 16d81c1
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import io.edurt.gcm.redis.client.RedisHashClient;
import io.edurt.gcm.redis.client.RedisListClient;
import io.edurt.gcm.redis.client.RedisSetClient;
import io.edurt.gcm.redis.client.RedisStringClient;
import io.edurt.gcm.redis.provider.RedisHashProvider;
import io.edurt.gcm.redis.provider.RedisListProvider;
import io.edurt.gcm.redis.provider.RedisSetProvider;
import io.edurt.gcm.redis.provider.RedisStringProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -51,12 +53,13 @@ public RedisModule()
@Override
protected void configure()
{
LOGGER.info("binding redis datasource configuration information is started.");
LOGGER.info("load configuration from local file {}", this.configuration);
LOGGER.info("Binding redis datasource configuration information is started.");
LOGGER.info("Load configuration from local file {}", this.configuration);
Properties configuration = PropertiesUtils.loadProperties(this.configuration);
LOGGER.info("binding redis datasource configuration information is completed, with a total of {} configurations", configuration.stringPropertyNames().size());
LOGGER.info("Binding redis datasource configuration information is completed, with a total of {} configurations", configuration.stringPropertyNames().size());
bind(RedisListClient.class).toProvider(new RedisListProvider(configuration)).in(Scopes.SINGLETON);
bind(RedisHashClient.class).toProvider(new RedisHashProvider(configuration)).in(Scopes.SINGLETON);
bind(RedisSetClient.class).toProvider(new RedisSetProvider(configuration)).in(Scopes.SINGLETON);
bind(RedisStringClient.class).toProvider(new RedisStringProvider(configuration)).in(Scopes.SINGLETON);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,60 @@ public synchronized Long delete(String key)
return jedis.del(key);
}
}

public synchronized Long persist(String key)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.persist(key);
}
}

public synchronized Long persist(byte[] key)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.persist(key);
}
}

public synchronized Long ttl(String key)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.ttl(key);
}
}

public synchronized Long ttl(byte[] key)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.ttl(key);
}
}

public synchronized Long expire(byte[] key, int seconds)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.expire(key, seconds);
}
}

public synchronized Long expire(String key, int seconds)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.expire(key, seconds);
}
}

public synchronized Long pexpire(String key, long milliseconds)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.pexpire(key, milliseconds);
}
}

public synchronized Long pexpire(byte[] key, long milliseconds)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.pexpire(key, milliseconds);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 io.edurt.gcm.redis.client;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisStringClient
extends RedisClient
{
public RedisStringClient(ShardedJedisPool pool)
{
super(pool);
}

public synchronized String set(String key, String value)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.set(key, value);
}
}

public synchronized String setByte(byte[] key, byte[] value)
{
try (ShardedJedis jedis = this.pool.getResource()) {
return jedis.set(key, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 io.edurt.gcm.redis.provider;

import io.edurt.gcm.redis.client.RedisStringClient;

import javax.inject.Provider;

import java.util.Properties;

public class RedisStringProvider
extends RedisProvider
implements Provider<RedisStringClient>
{
public RedisStringProvider(Properties configuration)
{
super(configuration);
}

@Override
public RedisStringClient get()
{
return new RedisStringClient(this.getJedisPool());
}
}

0 comments on commit 16d81c1

Please sign in to comment.