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.
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
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:
pip install ncache-client
And if you are using the Professional edition of NCache, then you will need to install the following package:
pip install ncache-professional-client
Next, include the following module in your Python application:
from ncache.client.cache import cache
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:
cache = ncache.CacheManager.get_cache(“myCache”)
except Exception as exp:
# handle any exceptions
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.
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.
# Pre-condition: Cache is already connected
# Get product from database against given product ID
product = fetch_product_from_db()
# Generate a unique cache key for this product
key = "Product:" + product.get_product_id()
# Add Product object to cache
version = cache.add(key, product)
# Get item against key from cache in given Class type
retrieved_item = cache.get(key, Product)
# Remove specified item
item_removed = cache.remove(key, Product)
except Exception as exp:
# Handle errors
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.
# Get customer from database
customer = fetch_customer_from_db("ALFKI")
# Create a unique cache key for this customer.
key = "Customers:" + customer.get_customer_id()
# Specify tags
tags = [
ncache.Tag("East Coast Customers"),
# Create a new CacheItem
cache_item = ncache.CacheItem(customer)
# Setting the tag property of the cacheItem
# Add customer object to cache
version = cache.add(key, cache_item)
except Exception as exp:
# Handle errors
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!