A
A
andy3010862017-08-28 19:55:37
ASP.NET
andy301086, 2017-08-28 19:55:37

Using Redis?

Hello everyone, I started to deal with radishes, read the documentation and a couple of questions arose:
The class for working with radishes
public class RedisStore
{
private const string EmptyResultValue = "EmptyResult";
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private static Lazy configOptions = new Lazy(() =>
{
var configOptions = new ConfigurationOptions();
configOptions.EndPoints.Add("localhost");
configOptions.ConnectTimeout = 100000;
configOptions.SyncTimeout = 100000;
configOptions.AbortOnConnectFail = false;
return configOptions;
});
private static Lazy connection
= new Lazy(
() => ConnectionMultiplexer.Connect(configOptions.Value));
private static ConnectionMultiplexer SafeConnection
{
get
{
return connection.Value;
}
}
#region Operations
///
/// Add item by key
///
///
///
///
///
public void AddItemByKey(string key, T item, TimeSpan? expiry = null)
{
try
{
var serializedItem = JSON.Serialize(item);
IDatabase db = SafeConnection.GetDatabase();
db.StringSetAsync(key, serializedItem, expiry);
}
catch (Exception ex)
{
_logger.ErrorException("Redis Exception: " + ex.Message, ex);
}
}
#endregion
}
Using this class in a controller
public ActionResult Index()
{
RedisStore redis = new RedisStore();
redis.AddItemByKey("key1", "value1");
redis.AddItemByKey("key2", "
ViewBag.Title = "Home Page";
returnView();
}
When I opened the radish console, it says that 3 clients are connected to the radish -
# Clients
connected_clients: 3
Questions:
1. am I working with radish correctly
2. why is a connection to radish created for each operation (and these connections are not closed until you restart the project )
3. Perhaps you need to fumble the radish at the global level of the application?
Thanks in advance to all who respond

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
denismaster, 2017-08-30
@denismaster

It is quite possible that you should pay attention to dependency injection in order to save code from type constructs: new RedisStore();
As for connections, you can use the Dispose pattern, as well as the view construct. Then the connections should be closed automatically. Try to find examples of working with radish on the Internet, in the same .NET Core there were already examples of services based on it. using (var store = new RedisStore()) { /*code*/ }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question