Create Client Cache with NuGet Packages Installation
If you're using NCache NuGet packages and want to create client cache from your application without having NCache installed on it, NCache allows you to do so with these steps:
Pre-requisites
The required NuGet package must be already installed in your application, as explained in the NCache Installation Guide. On successful installation of NuGet package, it copies these two configuration files in your application's local directory:
- config.ncconf: is used for local inproc cache and inproc client caches. For client cache, config.ncconf must also contain the name of the clustered cache that this client cache is a part of.
- client.ncconf: contains the information about the cache servers of each cache that the application needs to access.
Step 1: Edit NCache Configuration File
- Open the copied config.ncconf. The configuration file comes with a default local cache by the name of mycache.
- If you already have the configuration of client cache, add the configuration under the
<configuration>
tag. - If you do not have the configuration of client cache, copy the following configuration and paste it under the
<configuration>
tag.
Note
It is recommended that you use JSON serialization if you are using ASP.NET 5.0 and above. For more details click here.
<cache-config cache-name="demoClientCache" environment="" config-id="48bf4956-6d5b-4704-bb4a-254ca66a567b" config-version="0">
<cache-settings inproc="True" last-modified="" auto-start="False" data-format="Object" serialization="Binary">
<logging enable-logs="True" trace-errors="True" trace-notices="False" trace-warnings="False" trace-debug="False" log-path=""/>
<performance-counters enable-counters="True" snmp-port="0"/>
<compression enable-compression="False" threshold="100kb"/>
<client-death-detection/>
<client-activity-notification enabled="False" retention-period="5sec"/>
<cache-notifications item-remove="False" item-add="False" item-update="False" expiration-time="15sec"/>
<cleanup interval="15sec"/>
<storage type="heap" cache-size="1024mb"/>
<eviction-policy enabled-eviction="True" default-priority="normal" policy="lru" eviction-ratio="5%"/>
<expiration-policy enabled="False">
<absolute-expiration longer-enabled="False" longer-value="0" default-enabled="False" default-value="0"/>
<sliding-expiration longer-enabled="False" longer-value="0" default-enabled="False" default-value="0"/>
</expiration-policy>
<sql-dependency use-default="True"/>
<synchronization strategy="polling" polling-interval="10sec"/>
<cache-topology topology="client-cache"/>
<client-cache-settings server-cache="demoCache" persist-queue="False" enable-disconnected="False" offline-queue-size="0"/>
<tasks-config max-tasks="10" chunk-size="100" communicate-stats="False" queue-size="10" max-avoidable-exceptions="10"/>
<split-brain-recovery enable="False" detection-interval="60"/>
</cache-settings>
</cache-config>
Step 2: Update Cache Names in Cache Configuration
- Update the name of the client cache according to your own client cache name in the following tag in config.ncconf:
<cache-config cache-name="demoClientCache" ... />
- Change the name of the clustered cache that the client cache is part of and your application will be connecting to in config.ncconf:
<cache-settings>
<client-cache-settings server-cache="demoCache" ... />
</cache-settings>
Step 3: Create Cache Connection
After modifying the cache configuration, the client application needs to create the cache connection and that can be done by either of the two methods:
Method 1: Modify client.ncconf
From your application's local directory, open client.ncconf. Add the cache configurations as shown below:
- Clustered Cache:
This is the clustered cache that the client cache is part of and your application connects to. Make sure you provide your own cache's name as provided in step 2 and the IP for the cluster in the<server name = ... >
tag.
<cache id="demoCache" client-cache-id="demoClientCache" client-cache-syncmode="optimistic" default-readthru-provider="" default-writethru-provider="" load-balance="True" enable-client-logs="False" log-level="error">
<server name="20.200.20.29"/>
</cache>
- Client Cache:
Make sure that the client cache name is the same as you provided in step 2. Change the IP as your local machine IP in the<server name = ... >
tag.
<cache id="demoClientCache" client-cache-id="" client-cache-syncmode="optimistic" default-readthru-provider="" default-writethru-provider="" load-balance="True" enable-client-logs="False" log-level="error">
<server name="20.200.20.38"/>
</cache>
Method 2: Using Client Connection Options
You can connect to the cache and client cache using the GetCache method. For more detail, refer to the Connect to Clustered and Client Cache section in Programmers' Guide.
// Specify the cache names
string clusteredCache = "demoCache";
string clientCache = "demoClientCache";
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.LoadBalance = true;
cacheConnectionOptions.ConnectionRetries = 5;
CacheConnectionOptions clientCacheConnectionOptions = new CacheConnectionOptions();
clientCacheConnectionOptions.LoadBalance = true;
clientCacheConnectionOptions.ConnectionRetries = 5;
clientCacheConnectionOptions.Mode = IsolationLevel.OutProc;
// Connect to the caches in a single call
ICache cache = CacheManager.GetCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
You can now perform operations on your client cache easily.
See Also
Enable Client Cache on Client Nodes
Disable Client Cache on Client Nodes
Remove Client Cache
Management Operations