Skip to content

Commit d0691be

Browse files
committed
Add session timeout parameter for redis
1 parent d517390 commit d0691be

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

dotnet/src/dotnetcore/GxClasses/Services/Session/GXSessionFactory.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ public class GXSessionServiceFactory
1515
static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD";
1616
static string SESSION_SCHEMA = "SESSION_PROVIDER_SCHEMA";
1717
static string SESSION_TABLE_NAME = "SESSION_PROVIDER_TABLE_NAME";
18+
static string SESSION_TIMEOUT = "SESSION_PROVIDER_SESSION_TIMEOUT";
1819
public static ISessionService GetProvider()
1920
{
2021
var instance = GXServices.Instance?.Get(GXServices.SESSION_SERVICE);
2122
if (instance != null)
2223
{
2324
if (instance.Name.Equals(REDIS, StringComparison.OrdinalIgnoreCase))
2425
{
25-
return new GxRedisSession(instance.Properties.Get(SESSION_ADDRESS), CryptoImpl.Decrypt(instance.Properties.Get(SESSION_PASSWORD)), instance.Properties.Get(SESSION_INSTANCE));
26+
return new GxRedisSession(instance.Properties.Get(SESSION_ADDRESS), CryptoImpl.Decrypt(instance.Properties.Get(SESSION_PASSWORD)), instance.Properties.Get(SESSION_INSTANCE), int.Parse(instance.Properties.Get(SESSION_TIMEOUT)));
2627
}
2728
else if (instance.Name.Equals(DATABASE, StringComparison.OrdinalIgnoreCase))
2829
{
@@ -36,17 +37,19 @@ public static ISessionService GetProvider()
3637
}
3738
public class GxRedisSession : ISessionService
3839
{
39-
public GxRedisSession(string host, string password, string instanceName)
40+
public GxRedisSession(string host, string password, string instanceName, int sessionTimeout)
4041
{
4142
ConnectionString = $"{host}";
4243
if (!string.IsNullOrEmpty(password))
4344
{
4445
ConnectionString += $",password={password}";
4546
}
4647
InstanceName = instanceName;
48+
SessionTimeout = sessionTimeout;
4749
}
4850
public string ConnectionString { get; }
4951
public string InstanceName { get; }
52+
public int SessionTimeout { get; }
5053

5154
public string Schema => throw new NotImplementedException();
5255

@@ -71,6 +74,8 @@ public GxDatabaseSession(string host, string password, string schema, string tab
7174
public string TableName { get; }
7275

7376
public string InstanceName => throw new NotImplementedException();
77+
78+
public int SessionTimeout => throw new NotImplementedException();
7479
}
7580

7681
public interface ISessionService
@@ -79,5 +84,6 @@ public interface ISessionService
7984
string Schema { get; }
8085
string TableName { get; }
8186
string InstanceName { get; }
87+
int SessionTimeout { get; }
8288
}
8389
}

dotnet/src/dotnetframework/Providers/Cache/GxRedis/GxRedis.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ public sealed class Redis : ICacheService2
2121
IDatabase _redisDatabase;
2222
ConfigurationOptions _redisConnectionOptions;
2323
private const int REDIS_DEFAULT_PORT = 6379;
24-
24+
public int redisSessionTimeout;
2525
public Redis(string connectionString)
2626
{
2727
_redisConnectionOptions = ConfigurationOptions.Parse(connectionString);
2828
_redisConnectionOptions.AllowAdmin = true;
2929
}
30+
31+
public Redis(string connectionString, int sessionTimeout)
32+
{
33+
_redisConnectionOptions = ConfigurationOptions.Parse(connectionString);
34+
_redisConnectionOptions.AllowAdmin = true;
35+
redisSessionTimeout = sessionTimeout;
36+
}
3037
public Redis()
3138
{
3239
GXService providerService = ServiceFactory.GetGXServices().Get(GXServices.CACHE_SERVICE);

dotnet/src/extensions/Azure/Handlers/HttpHandler/HttpTriggerHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public void Remove(string key)
9696

9797
public void Set(string key, byte[] value)
9898
{
99-
_redis.Set(Id, key, value, AZURE_SESSION_TIMEOUT_IN_MINUTES);
99+
if (_redis.redisSessionTimeout != 0)
100+
_redis.Set(Id, key, value, _redis.redisSessionTimeout);
101+
else
102+
_redis.Set(Id, key, value, AZURE_SESSION_TIMEOUT_IN_MINUTES);
100103
}
101104

102105
public bool TryGetValue(string key, out byte[] value)

dotnet/src/extensions/Azure/Handlers/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ static async Task Main()
3535
.ConfigureServices(services =>
3636
{
3737
ISessionService sessionService = GXSessionServiceFactory.GetProvider();
38-
string connectionString = "";
3938
if (sessionService is GxRedisSession)
4039
{
41-
connectionString = sessionService.ConnectionString;
42-
services.AddSingleton<ICacheService2>(x => new Redis(connectionString));
43-
//ConnectionMultiplexer.SetFeatureFlag("preventthreadtheft", true);
40+
services.AddSingleton<ICacheService2>(x => new Redis(sessionService.ConnectionString, sessionService.SessionTimeout));
4441
}
4542
else
4643
services.AddSingleton<ICacheService2>(x => new InProcessCache());

0 commit comments

Comments
 (0)