In an Azure Function, I have a c# program that processes tab delimited data into JSON entries. I'd like to improve it in terms of performance and the connection management.
I want to remove personal data from each line.* As each line gets prepped for turning into JSON I perform this function for two values.** I need to work with the depersonalised data but the operational systems will need to request data from the system. They have access to the PII but the analytical platform (downstream of this function) will not have it. The Key Value pairs are therefore constructed so that the operational system and the log ingestion processes can extract guids. Each key is like AccountID:PIItype:PII and then each value is a guid.***
If I've previously depersonalised a value, I want to retrieve the previously provided value from the Azure Redis Cache. If it doesn't exist, add it to the cache.
using StackExchange.Redis;
using System.Text;
using System.Threading;
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
ThreadPool.SetMinThreads(50, 50);
string redisCacheName = System.Environment.GetEnvironmentVariable("rediscache_name", EnvironmentVariableTarget.Process).ToString();
string redisCachePassword = System.Environment.GetEnvironmentVariable("rediscache_password", EnvironmentVariableTarget.Process).ToString();
return ConnectionMultiplexer.Connect(redisCacheName + ",abortConnect=false,ssl=true,password=" + redisCachePassword);
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
static string depersonalise_value(string input, string field, int account_id)
{
IDatabase cache = Connection.GetDatabase();
string depersvalue = $"{account_id}:{field}:{input}";
string existingguid = (string)cache.StringGet(depersvalue);
if (String.IsNullOrEmpty(existingguid))
{
string value = $"{account_id}{Guid.NewGuid()}";
cache.StringSet(depersvalue, value);
return (value);
}
else return (existingguid);
}
* Hashing is something considered but discarded
** The two values are independent of each other
*** There is a redis cache per region so can't rely on uniqueness of the guid by host. This is just a simple preventative as an account can only come from a single region.