Create Client Cache with NuGet Packages Installation
If you're using NCache NuGet packages and want to create a Client Cache using your application without having NCache installed, then NCache allows you to do so in the following steps:
Prerequisites for Creating Client Cache
The required NuGet package must be already installed in your application. On successful installation of the NuGet package, it copies these two configuration files in your application's local directory:
- config.ncconf: It is used for local InProc cache and InProc Client Caches. For the Client Cache, the config.ncconf file must also contain the name of the clustered cache that this Client Cache is a part of.
- client.ncconf: It contains information about the cache servers of each cache that the application needs to access.
Step 1: Edit NCache Configuration File
- Open the configurations copied from the config.ncconf file. The configuration file comes with a default local cache by the name of mycache.
- If you already have the configuration of the Client Cache, add the configuration under the
<configuration>
tag. - If you do not have the configuration of the Client Cache, copy the following configuration and paste it under the
<configuration>
tag.
Note
Ensure that you are using the JSON Serialization format for .NET 8.0, as BinaryFormatter Serialization methods are obsolete and prohibited in .NET 8.0. However, if you still want to use Binary Serialization, you can suppress the BinaryFormatter, as discussed here.
Regular Client Cache
Following is the configuration for the Regular Client Cache.
<cache-config cache-name="demoClientCache" store-type="distributed-cache" environment="" config-id="4b121daa-7d50-49dc-9b68-c67889eaa084" config-version="0">
<cache-settings inproc="False" last-modified="" auto-start="False" data-format="Serialized" 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"/>
<data-load-balancing enabled="False" auto-balancing-threshold="60%" auto-balancing-interval="30sec"/>
<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" type="regular" 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>
Full Data Client Cache
Important
If you make any configuration changes to the Full-Data Client Cache, you must restart the Client Cache.
Following is the configuration for the Full-Data Client Cache.
<cache-config cache-name="demoClientCache" store-type="distributed-cache" environment="" config-id="8898e176-baac-4d06-bb2d-0499eadf397f" 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"/>
<data-load-balancing enabled="False" auto-balancing-threshold="60%" auto-balancing-interval="30sec"/>
<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" type="full-data" 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 the Tags in Cache Configuration
- Update the name of the Client Cache according to your own Client Cache name in the following tag in the config.ncconf file:
<client-cache name="democlientCache" ... />
- Change the name of the clustered cache that the Client Cache is a part of, and with which your application will connect to in the config.ncconf file:
<cache-setting>
<client-cache-settings server-cache="demoCache" ... />
</cache-setting>
- Update the Reference Data Types in the clustered cache configuration by updating the following tag in the config.ncconf file:
<client-cache name="democlientCache">
<ref-datatypes enabled="True" failQueryOnPartialDataset="True" disableL2FallbackOnMiss="True" data-reload-threshold="20%">
<ref-datatype type-name="Alachisoft.NCache.Sample.Data.Customer"/>
</ref-datatypes>
</client-cache>
After modifying the cache configuration, the client application will need to create the cache connection and that can be done by the following two methods:
Method 1: Modify client.ncconf
From your application's local directory, open client.ncconf. Add the cache configurations as shown below for the clustered cache that the Client Cache is a part of and with which your application connects. Make sure you provide your own cache's name as provided in step 2 and the IP for the cluster in the <server ip = ... >
tag.
<store name="clusteredcache">
<servers>
<server ip="20.200.20.40"/>
</servers>
<client-balancing enabled="False"/>
<client-cache-options name="clientCache" syncmode="optimistic" skip-if-unavailable="True" reconnect-interval="10"/>
<client-logs enabled="False" level="error"/>
</store>
Method 2: Using Client Cache Connection Options
You can connect to the cache and Client Cache using the GetCache method. For more details, refer to the Connect to Cache section in the Programmer's 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