- 浏览: 2552360 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 574NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 266Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
还在测试,这几天完善一下
《C# MemCache监控管理工具详解》 在IT行业中,高效的数据存储和访问是系统性能的关键因素之一。MemCache作为一种广泛使用的内存缓存系统,它能够显著提高应用程序的数据读取速度,减轻数据库的压力。然而,如何...
使用组件memcached 1.2.6.net 类库 memcacheddotnet_clientlib-1.1.5 1.增加memcacheddotnet_clientlib-1.1.5代码 下载好组件后,用vs打开.net类库memcacheddotnet_clientlib-1.1.5,打开MemCachedClient.cs,增加如下...
.NET Core中的MemoryCache是一种用于在应用程序内存中存储数据的缓存机制,以提高数据访问速度和应用程序性能。MemoryCache属于.NET Core的内置缓存机制之一,广泛应用于各种需要快速数据存取的场景。本篇文章主要...
ASP.NET应用程序的两层缓存策略是提高应用性能的关键技术之一,主要涉及到内存中的`MemoryCache`和分布式缓存服务`Redis`。本文将详细探讨这两种缓存机制以及如何在C#开发环境中结合使用它们。 首先,我们来了解`...
在C#中,缓存可以分为多种类型,包括进程内缓存(如MemoryCache)和分布式缓存(如Redis、Memcached)。本文将重点介绍进程内缓存,即MemoryCache类,它是C#中最常用的缓存实现。 MemoryCache类是.NET Framework ...
我们可以使用Microsoft.Extensions.Caching命名空间下的各种类,如MemoryCache(内存缓存)、DistributedCache(分布式缓存)等。MemoryCache类似于.NET Framework中的Cache,而DistributedCache则适合跨服务器的...
使用`MemoryCache`类是`System.Runtime.Caching`的主要方式,可以通过实例化`MemoryCache.Default`或创建自定义的`MemoryCache`实例来存取数据。`MemoryCache`提供了丰富的API,如设置过期时间、添加移除缓存项、...
2. .NET Core的`Microsoft.Extensions.Caching`:适用于跨平台应用,包括MemoryCache和 DistributedCache(如Redis)。 ```csharp var cache = new MemoryCache(new MemoryCacheOptions()); cache.Set("key", "value...
"利用MemoryCache和AOP进行昂贵的呼叫"这个主题聚焦于两种技术:System.Runtime.Caching.MemoryCache和面向切面编程(AOP)。这两种技术结合使用,可以有效地缓存计算昂贵或数据库查询结果,避免重复执行这些操作,...
C#中可以通过`System.Runtime.Caching`命名空间下的`MemoryCache`类实现这些策略。 2. **键值对存储**:缓存通常基于键值对(Key-Value Pair)存储数据,键是唯一标识符,值是实际的文件内容。我们可以使用`...
MemoryCache.Add(key, value, DateTimeOffset.UtcNow.Add(expiration)); } // 获取缓存方法 public static object GetFromCache(string key) { return MemoryCache.Get(key); } // 删除缓存方法 public ...
考虑到性能优化,C#还提供了缓存机制,如MemoryCache,可以用来存储频繁查询的结果,减少不必要的数据库访问。同时,为了确保数据一致性,可能还需要引入并发控制和事务管理,C#的System.Transactions命名空间提供了...
- **容量限制**:可以通过`MemoryCache.SetCacheSize`方法设定缓存的最大容量,超过该容量时,`MemoryCache`会根据内部的LRU(Least Recently Used)算法自动清除最近最少使用的条目。 - **依赖项**:可以设置缓存...
C#的async/await关键字和MemoryCache等工具可以帮助实现这一点。 通过深入学习和实践,开发者能够运用C#和百度地图URI API v2.0构建高效、功能齐全的地图应用。这涵盖了从基础的定位和地图展示,到复杂的路径规划和...
C#中的MemoryCache或第三方库如Redis可实现缓存功能。 综上所述,C#在移动数据库记录操作方面提供了丰富的工具和库,涵盖了从基本的SQL命令执行到复杂的ORM和异步操作。开发者可以根据项目需求选择合适的技术栈,以...
- **缓存技术**:使用内存缓存(如MemoryCache)来减少对数据库的频繁访问,提高系统响应速度。 - **异步编程**:利用async/await关键字进行异步操作,提升用户体验。 通过对C#考勤管理系统源码的深入分析,我们...
9. **缓存技术**:对于频繁访问的数据,如热门菜品,可以利用C#的缓存机制(如MemoryCache或Redis)来提高响应速度,减轻数据库压力。 10. **负载均衡与集群**:当订餐系统用户量增大时,可以采用负载均衡策略,如...
12. **缓存策略**:为了提高性能,源码可能应用了内存缓存(如Redis或Memory Cache)来存储频繁访问的数据。 通过深入学习和研究这个C#的OA办公管理系统源码,开发者不仅可以提升C#编程技能,还能了解企业级应用...