阅读更多
C# Memory Cache
This is the online Tool to try my C# codes
https://dotnetfiddle.net/
Recently I am study C#. Haha, I use C# to build an IIS Service to provide HTTP RESTful API.
In side the functions, we need to use dll to connect to a very old ERP system. The connection takes a long time to init. So we decide to Cache that connection.
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.caching.memorycache?redirectedfrom=MSDN&view=netframework-4.7.2
We need the connection to expire after 1 hour
http://zerotiem22.blogspot.com/2018/03/c-cache_27.html
We need to set up the dll to make it working
https://stackoverflow.com/questions/13615407/no-system-runtime-caching-available
In solution explorer, right-click on “References”
Select “Add Reference”
From left side menu select “Assemblies”
Look for (or filter) and add System.Runtime.Caching.dll.
Add callback method to close the connection when the cache object expire
https://www.codeproject.com/Articles/290935/Using-MemoryCache-in-Net-4-0
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.caching.cacheentryremovedcallback?view=netframework-4.7.2
Here is the core codes for the Cache Solution
using System;
using SAPbobsCOM;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Caching;
using System.Net.Http;
private static ObjectCache cache = MemoryCache.Default;
public static SAPbobsCOM.Company SAPCompany(HttpRequestMessage request)
{
AccountInfo accountInfo = GetAccountInfoFromHeadersOrConfig(request);
var hashKey = accountInfo.GetHashCode().ToString();
SAPbobsCOM.Company companyObj = cache[hashKey] as SAPbobsCOM.Company;
if (companyObj == null || !companyObj.Connected)
{
CacheItemPolicy policy = new CacheItemPolicy();
CacheEntryRemovedCallback callback = new CacheEntryRemovedCallback(ConnectionRemovedCallback);
policy.SlidingExpiration = TimeSpan.FromSeconds(60 * 60); //1 hour
policy.RemovedCallback = callback;
companyObj = Initialize(accountInfo);
cache.Set(hashKey, companyObj, policy);
}
return companyObj;
}
private static void ConnectionRemovedCallback(CacheEntryRemovedArguments arguments) {
SAPbobsCOM.Company companyObj = (SAPbobsCOM.Company)arguments.CacheItem.Value;
companyObj.Disconnect();
}
Here is the overwritten about the ToString and GetHashCode methods
using System;
namespace RESTServices.BusLayer.Utils
{
public class AccountInfo
{
public string Server { get; set; }
public string DB { get; set; }
public int DBType { get; set; }
public string SAPUserName { get; set; }
public string SAPPassword { get; set; }
public string LicenseServer { get; set; }
public bool DBTrustedConnection { get; set; }
public bool IsHANA { get; set; }
public string DBUser { get; set; }
public string DBPassword { get; set; }
public AccountInfo()
{
}
public override string ToString() {
return "|Server:" + Server +
"|DB:" + DB +
"|DBType:" + DBType +
"|SAPUserName:" + SAPUserName +
"|SAPPassword:" + SAPPassword +
"|LicenseServer:" + LicenseServer +
"|DBTrustedConnection:" + DBTrustedConnection +
"|IsHANA:" + IsHANA +
"|DBUser:" + DBUser +
"|DBPassword:" + DBPassword;
}
public override int GetHashCode() {
return this.ToString().GetHashCode();
}
}
}
Convert String to Boolean in C#
private static bool ToBoolean(string str)
{
try
{
return Convert.ToBoolean(str);
}
catch { }
try
{
return Convert.ToBoolean(Convert.ToInt32(str));
}
catch { }
return false;
}
Deal with HTTP Header
//sap_password
if (headers.Contains(HEADER_SAP_PASSWORD))
{
var sapPasswrod = headers.GetValues(HEADER_SAP_PASSWORD).First();
accountInfo.SAPPassword = sapPasswrod;
}
References:
https://dotnetfiddle.net/
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.caching.memorycache?redirectedfrom=MSDN&view=netframework-4.7.2