Skip to content

Commit

Permalink
Distinguish RedisClusterAdapter from RedisAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Heromyth committed May 27, 2021
1 parent 25b51fd commit d9b0945
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 51 deletions.
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "Apache-2.0",
"versions": ["WITH_HUNT_CACHE"],
"dependencies": {
"hunt-redis": "~>1.3.0"
"hunt-redis": "~>1.3.2"
},
"configurations":[
{
Expand Down
76 changes: 44 additions & 32 deletions source/hunt/cache/Cache.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ final class Cache
return;
}

if(className == typeid(RedisClusterAdapter))
{
_redisClusterAdapter = cast(RedisClusterAdapter)adapterObject;
_type = CACHE_ADAPTER.REDIS_CLUSTER;
return;
}

version(WITH_HUNT_MEMCACHE)
{
if(className == typeid(MemcacheAdapter))
Expand Down Expand Up @@ -74,10 +81,10 @@ final class Cache
return get!(MemoryAdapter, V)(key);

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
return get!(RedisClusterAdapter, V)(key);
else
return get!(RedisAdapter, V)(key);
return get!(RedisAdapter, V)(key);

case CACHE_ADAPTER.REDIS_CLUSTER:
return get!(RedisClusterAdapter, V)(key);

version(WITH_HUNT_MEMCACHE)
{
Expand Down Expand Up @@ -134,10 +141,10 @@ final class Cache
return get!(MemoryAdapter, V)(keys);

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
return get!(RedisClusterAdapter, V)(keys);
else
return get!(RedisAdapter, V)(keys);
return get!(RedisAdapter, V)(keys);

case CACHE_ADAPTER.REDIS_CLUSTER:
return get!(RedisClusterAdapter, V)(keys);

version(WITH_HUNT_MEMCACHE)
{
Expand Down Expand Up @@ -182,10 +189,10 @@ final class Cache
return hasKey!MemoryAdapter(key);

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
return hasKey!RedisClusterAdapter(key);
else
return hasKey!RedisAdapter(key);
return hasKey!RedisAdapter(key);

case CACHE_ADAPTER.REDIS_CLUSTER:
return hasKey!RedisClusterAdapter(key);

version(WITH_HUNT_MEMCACHE)
{
Expand Down Expand Up @@ -222,10 +229,10 @@ final class Cache
return set!(MemoryAdapter, V)(key, v, expired);

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
return set!(RedisClusterAdapter, V)(key, v, expired);
else
return set!(RedisAdapter, V)(key, v, expired);
return set!(RedisAdapter, V)(key, v, expired);

case CACHE_ADAPTER.REDIS_CLUSTER:
return set!(RedisClusterAdapter, V)(key, v, expired);

version(WITH_HUNT_MEMCACHE)
{
Expand Down Expand Up @@ -289,11 +296,13 @@ final class Cache
{
case CACHE_ADAPTER.MEMORY:
return set!(MemoryAdapter, V)(maps, expired);

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
return set!(RedisClusterAdapter, V)(maps, expired);
else
return set!(RedisAdapter, V)(maps, expired);
return set!(RedisAdapter, V)(maps, expired);

case CACHE_ADAPTER.REDIS_CLUSTER:
return set!(RedisClusterAdapter, V)(maps, expired);

case CACHE_ADAPTER.MEMCACHE_ADAPTER:
return set!(MemcacheAdapter, V)(maps, expired);
case CACHE_ADAPTER.ROCKSDB:
Expand Down Expand Up @@ -323,10 +332,10 @@ final class Cache
return remove!MemoryAdapter(key);

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
return remove!RedisClusterAdapter(key);
else
return remove!RedisAdapter(key);
return remove!RedisAdapter(key);

case CACHE_ADAPTER.REDIS_CLUSTER:
return remove!RedisClusterAdapter(key);

version(WITH_HUNT_MEMCACHE)
{
Expand Down Expand Up @@ -368,10 +377,11 @@ final class Cache
break;

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
remove!RedisClusterAdapter(keys);
else
remove!RedisAdapter(keys);
remove!RedisAdapter(keys);
break;

case CACHE_ADAPTER.REDIS_CLUSTER:
remove!RedisClusterAdapter(keys);
break;

version(WITH_HUNT_MEMCACHE)
Expand Down Expand Up @@ -418,10 +428,11 @@ final class Cache
break;

case CACHE_ADAPTER.REDIS:
if(_option.isRedisClusterEnabled)
clear!RedisClusterAdapter();
else
clear!RedisAdapter();
clear!RedisAdapter();
break;

case CACHE_ADAPTER.REDIS_CLUSTER:
clear!RedisClusterAdapter();
break;

version(WITH_HUNT_MEMCACHE)
Expand Down Expand Up @@ -487,6 +498,7 @@ final class Cache

MemoryAdapter _memoryAdapter;
RedisAdapter _redisAdapter;
RedisClusterAdapter _redisClusterAdapter;
version(WITH_HUNT_MEMCACHE) MemcacheAdapter _memcacheAdapter;
version(WITH_HUNT_ROCKSDB) RocksdbAdapter _rocksdbAdapter;

Expand Down
9 changes: 4 additions & 5 deletions source/hunt/cache/CacheFactory.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ class CacheFactory
return new Cache(memoryAdapter, option);

case AdapterType.REDIS:
if(option.isRedisClusterEnabled) {
return new Cache(new RedisClusterAdapter(option.redisPool), option, memoryAdapter);
} else {
return new Cache(new RedisAdapter(option.redisPool), option, memoryAdapter);
}
return new Cache(new RedisAdapter(option.redisPool), option, memoryAdapter);

case AdapterType.REDIS_CLUSTER:
return new Cache(new RedisClusterAdapter(option.redisPool, option.redisCluster), option, memoryAdapter);

version(WITH_HUNT_MEMCACHE)
{
Expand Down
1 change: 1 addition & 0 deletions source/hunt/cache/CacheOptions.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CacheOptions {
string prefix = "";

bool isRedisClusterEnabled = false;

bool useSecondLevelCache = false;
uint maxEntriesLocalHeap = 10000;
bool eternal = false;
Expand Down
2 changes: 2 additions & 0 deletions source/hunt/cache/Defined.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum CACHE_ADAPTER
{
MEMORY,
REDIS,
REDIS_CLUSTER,
MEMCACHE,
ROCKSDB
}
Expand All @@ -18,6 +19,7 @@ enum CACHE_ADAPTER
enum AdapterType {
MEMORY = "memory",
REDIS = "redis",
REDIS_CLUSTER = "redis-cluster",
MEMCACHE = "memcache",
ROCKSDB = "rocksdb"
}
52 changes: 39 additions & 13 deletions source/hunt/cache/adapter/RedisClusterAdapter.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import hunt.cache.CacheOptions;
import hunt.cache.Nullable;
import hunt.cache.Store;

import hunt.collection.HashSet;
import hunt.collection.Set;
import hunt.Exceptions;
import hunt.logging;
import hunt.redis;
Expand All @@ -15,21 +17,45 @@ import std.string;

class RedisClusterAdapter : Adapter
{
this(RedisPoolConfig config)
this(RedisPoolConfig poolConfig, RedisClusterConfig clusterConfig)
{
try
{
_redis = new RedisCluster(new HostAndPort(config.host, config.port));
// _redis.connect();

// if (!config.password.empty())
// _redis.auth(config.password);
// _redis.select(config.database);
}
catch (Exception e)
{
logError(e);
// try
// {
// _redis = new RedisCluster(new HostAndPort(poolConfig.host, poolConfig.port));

// // if (!poolConfig.password.empty())
// // _redis.auth(poolConfig.password);
// // _redis.select(poolConfig.database);
// }
// catch (Exception e)
// {
// logError(e);
// }

Set!(HostAndPort) clusterNode = new HashSet!(HostAndPort)();
string[] hostPorts = clusterConfig.nodes;

foreach(string item; hostPorts) {
string[] hostPort = item.split(":");
if(hostPort.length < 2) {
warningf("Wrong host and port: %s", item);
continue;
}

version(HUNT_DEBUG) {
tracef("Cluster host: %s", hostPort);
}

try {
int port = to!int(hostPort[1]);
clusterNode.add(new HostAndPort(hostPort[0], port));
} catch(Exception ex) {
warning(ex);
}
}

_redis = new RedisCluster(clusterNode, poolConfig.connectionTimeout, poolConfig.soTimeout,
clusterConfig.redirections, poolConfig.password, poolConfig);
}

Nullable!V get(V) (string key)
Expand Down

0 comments on commit d9b0945

Please sign in to comment.