Python is an open-source, dynamically typed, and cross-platform programming language. Its high-level built-in data structures with dynamic binding and typing make it attractive for Rapid Application Development. It’s also used as the main scripting language for automating tasks and conducting data analysis.

NCache is an in-memory, linearly scalable, distributed cache, that stores data for better performance and higher scalability. It is a native .NET cache that does wonders when it comes to fast data access for your application’s improved performance and offers client-side APIs for a multitude of languages, Python being one of them. The Python client can be used with NCache and includes various features such as basic operations, data expiration techniques, locking, and much more.

NCache Details                                Download NCache                                NCache Client Side API

Why Use Python With NCache?

NCache, along with its vast features and enhanced caching capabilities, gives flexible options for you to use with Python. NCache will enhance your Python applications in the following ways:

  • Improved Performance: Data is stored in an in-memory cache, yielding maximum performance and faster response times.
  • Reliability: If an application server goes down or somehow data is lost, NCache provides reliability with the help of data replication.
  • Scalability: NCache provides linear scalability by allowing you to add more cache servers when the transaction load grows. For example, while using your Python application, you can instantly add a new cache server depending on the situation and continue to serve more requests without having to stop your application.
  • High-Availability and Fault Tolerance: When used in the context of a web farm, NCache provides better fault tolerance by keeping the data available across all server nodes in a clustered cache with no single point of failure.
  • Cluster Topologies: NCache offers different caching topologies to make your Python applications more reliable and scalable. For example, Replicated Topology provides load balancing so that your application can handle intensive traffic and Partitioned-Replica Topology provides higher reliability and scalability for faster transactions.

Python Client with NCache

NCache Details                                 NCache’s Core Capabilites                                     NCache Architecture

Python Client With NCache

The client requires a Python version greater than or equal to 3.5 installed to use it with NCache. The following steps include a detailed guide on how to get started with the Python client:

Step 1: Installing NCache

The very first step is to install NCache in your development environment. NCache has support for both Linux and Windows. In addition to that NCache offers three versions, Enterprise, Professional, and Open-Source. You can check out the detailed comparison of the three versions, to choose which version suits you best.

You can refer to the official installation guide, for step-by-step guidance on how to install NCache. Once you have done that, you can move to the next step.

Step 2: Creating a Clustered Cache

The next step is to create a clustered cache. Depending on your requirements, you can choose to add as many or as few nodes as you want. However, for demo purposes, we will recommend a two node setup, but a single node setup will also suffice, but without the data replication qualities of NCache.

NCache offers various topologies and cache configuration options, which vary based on the version of NCache you are using. You can check out the official documentation for creating a clustered cache. Once you have successfully created the cache, make sure to start the cache, and then you will be well on your way to starting the actual coding aspect of this blog.

Step 3: Configuring Python Client With NCache

The first thing to do is to fetch the Python package named ncache-client via pip if you are using the Enterprise edition of NCache. The command to install the package is given below. This package installs all the required client libraries for your Python application:

And if you are using the Professional edition of NCache, then you will need to install the following package:

Next, include the following module in your Python application:

Now we will need to connect to the cache. For this you only need to know the name of the cache, the rest will be handled by the client. Next, include the following module in your Python application:

And that’s it. You have successfully connected with the cache and your Python application is ready to be used with NCache. Next, we will look at some sample code, to showcase some basic features of NCache.

NCache Details                                 Client-Side Operations                                NCahce Docs

Some Basic Operations

The code below shows how you can add, get and remove an object from NCache. The data can be a single item or multiple items and can be synchronously or asynchronously inserted, updated, or removed from the cache.

Searching Cached Data

NCache provides the ability to search cached data through named tags, groups, as well as SQL-like queries. You can associate multiple keywords with cache items in NCache and these will act as identifying markers for cache items. Data can be retrieved based on these keywords through tags. For the advanced level of tagging where you have to query data related to a specific tag, you need named tags.

Cached data can be indexed and then queried using its SQL-like mechanism. It lets you search for keys fulfilling the given criteria and then return keys to the result set. Items in a cache having relation between them or falling under the same category can be grouped using NCache “Group” feature. Groups can logically partition data for better efficiency.

The following example adds a CacheItem containing object ‘customer’ to the cache using Insert method. It then sets an additional tag property against it by adding tags.

Conclusion

NCache can be easily and seamlessly integrated into your existing Python applications and with just a few steps you can also experience the extreme speed and reliability of NCache, while also providing scalability. To get started with all of this, download NCache now!

NCache Details                                  Download NCache                                 Edition Comparison

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment