Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>bin\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Alachisoft.NCache.SDK" Version="5.3.1" />
</ItemGroup>

<ItemGroup>
<None Update="client.ncconf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="config.ncconf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="tls.ncconf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
75 changes: 75 additions & 0 deletions playground/dotnetcore/BasicCacheOperations/Code/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Alachisoft.NCache.Client;
using Alachisoft.NCache.Runtime.Caching;

namespace NCache.Playground.Samples.BasicCacheOperations
{
// Basic caching operations (CRUD) as an intro to NCache API

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Sample showing basic caching operations (CRUD) in NCache, along with expiration.\n");

string cacheName = args[0];

Console.WriteLine($"Connecting to cache: {cacheName}\n");
// Connect to the cache and return a cache handle
var cache = CacheManager.GetCache(cacheName);
Console.WriteLine($"Connected to Cache successfully: {cacheName}\n");

// Add product 10001 to the cache with a 5-min expiration
Product prod1 = new Product { ProductID = 10001, Name = "Laptop", Price = 1000, Category = "Electronics" };

string prod1Key = prod1.ProductID.ToString();
var productCacheItem = new CacheItem(prod1);
productCacheItem.Expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(5));

// Add to the cache with 5min absolute expiration (TTL). Item expires automatically
cache.Add(prod1Key, productCacheItem);

Console.WriteLine("Product 10001 added successfully (5min expiration):");
PrintProductDetails(prod1);

// Get from the cache. If not found, a null is returned
Product retrievedprod1 = cache.Get<Product>(prod1Key);
if (retrievedprod1 != null)
{
Console.WriteLine("Product 10001 retrieved successfully:");
PrintProductDetails(retrievedprod1);
}

retrievedprod1.Price = 3000;

// Update product in the cache. If not found, it is added
productCacheItem = new CacheItem(retrievedprod1);
productCacheItem.Expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(5));
cache.Insert(prod1Key, productCacheItem);

Console.WriteLine("Price for Product 10001 updated successfully:");
PrintProductDetails(retrievedprod1);

// Remove the item from the cache
cache.Remove(prod1Key);
Console.WriteLine($"Deleted the product {prod1Key} from the cache...\n");

Console.WriteLine("Sample completed successfully.");
}

private static void PrintProductDetails(Product product)
{
Console.WriteLine("ProductID, Name, Price, Category");
Console.WriteLine($"- {product.ProductID}, {product.Name}, {product.Price}, {product.Category} \n");
}
}

// Sample class for Product
[Serializable]
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"BasicCacheOperations": {
"commandName": "Project",
"commandLineArgs": "demoCache"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>bin\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alachisoft.NCache.SDK" Version="5.3.1" />
</ItemGroup>

</Project>
72 changes: 72 additions & 0 deletions playground/dotnetcore/CacheItemLocking/Code/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Alachisoft.NCache.Client;

namespace NCache.Playground.Samples.CacheItemLocking
{
// Locking an item prevents other users from accessing it while you update it.
// You can lock with cache.GetCacheItem() or cache.Lock() and unlock with cache.Insert() or cache.Unlock()
// NOTE: Locking API is for application level locking. NCache already has its own internal locking
// for concurrency and thread-safe operations.

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Sample showing item level lock and unlock in NCache.\n");
string cacheName = args[0];

Console.WriteLine($"Connecting to cache: {cacheName}\n");
// Connect to the cache and return a cache handle
var cache = CacheManager.GetCache(cacheName);
Console.WriteLine($"Connected to Cache successfully: {cacheName}\n");

Product prod1 = new Product { ProductID = 11001, Name = "Laptop", Price = 1000, Category = "Electronics" };

string prod1Key = prod1.ProductID.ToString();
Console.WriteLine("Adding product 11001 to the cache...");

// add a product to the cache. if it already exists, the operation will fail
cache.Insert(prod1Key, prod1);
Console.WriteLine("Product added successfully:");
PrintProductDetails(prod1);

// Get product 11001 and lock it in the cache
Console.WriteLine("Get and lock product 11001 in the cache...");
LockHandle lockHandle = null;

// Passing "acquireLock:" flag also locks the item and "lockTimeout:" releases the lock after 10sec
var lockedCacheItem = cache.GetCacheItem(prod1Key, acquireLock: true, lockTimeout: TimeSpan.FromSeconds(10), ref lockHandle);
if (lockedCacheItem != null)
{
Console.WriteLine("Product 11001 retrieved and locked successfully:");
var retrievedProduct = lockedCacheItem.GetValue<Product>();
PrintProductDetails(retrievedProduct);

// Change the price of product 11001
retrievedProduct.Price = 2000;

// Insert() updates the item and releases the lock
cache.Insert(prod1Key, lockedCacheItem, lockHandle, releaseLock: true);
Console.WriteLine("Updated price of product 11001 and released the lock simultaneously in the cache...");

PrintProductDetails(retrievedProduct);
}
Console.WriteLine("Sample completed successfully.");
}

private static void PrintProductDetails(Product product)
{
Console.WriteLine("ProductID, Name, Price, Category");
Console.WriteLine($"- {product.ProductID}, {product.Name}, {product.Price}, {product.Category} \n");
}
}

// Sample class for Product
[Serializable]
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"CacheItemLocking": {
"commandName": "Project",
"commandLineArgs": "demoCache"
}
}
}
18 changes: 18 additions & 0 deletions playground/dotnetcore/DataStructures/Code/DataStructures.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>bin\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alachisoft.NCache.SDK" Version="5.3.1" />
</ItemGroup>

</Project>
129 changes: 129 additions & 0 deletions playground/dotnetcore/DataStructures/Code/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using Alachisoft.NCache.Client;
using Alachisoft.NCache.Client.DataTypes.Collections;

namespace NCache.Playground.Samples.DataStructures
{
// Use NCache distributed data structures (List and Queue in this sample). NCache provides
// Set, Dictionary, and Counter data structures as well. These data structures are distributed in the cluster
// for scalability and accessible to all clients.

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sample showing distributed data structures in NCache (List, Queue) for more powerful data management.\n");

var cacheName = args[0];

Console.WriteLine($"Connecting to cache: {cacheName}\n");
// Connect to the cache and return a cache handle
var cache = CacheManager.GetCache(cacheName);
Console.WriteLine($"Connected to Cache successfully: {cacheName}\n");

Console.WriteLine("Create a List data structure and add products 'Electronics' to it...");

// Get the list if it already exists in the cache
IDistributedList<Product> productList = cache.DataTypeManager.GetList<Product>("productList");

if (productList == null)
{
// Create a List data structure in the cache and then add products to it
productList = cache.DataTypeManager.CreateList<Product>("productList");
}

// These products are directly added to the cache inside the List data structure when you add them below
productList.Add(new Product { ProductID = 15001, Name = "Laptop", Price = 2000, Category = "Electronics" });
productList.Add(new Product { ProductID = 15002, Name = "Kindle", Price = 1000, Category = "Electronics" });
productList.Add(new Product { ProductID = 15004, Name = "Smart Phone", Price = 1000, Category = "Electronics" });
productList.Add(new Product { ProductID = 15005, Name = "Mobile Phone", Price = 200, Category = "Electronics" });

PrintProductList(productList);

Console.WriteLine("Create a Queue data structure and add orders to it...");

// Get the queue if it already exists in the cache
IDistributedQueue<Order> orderQueue = cache.DataTypeManager.GetQueue<Order>("orderQueue");

if (orderQueue == null)
{
// Create a Queue data structure and then add orders to it by maintaining their sequence
orderQueue = cache.DataTypeManager.CreateQueue<Order>("orderQueue");
}

// These orders are directly added to the cache inside the Queue data structure when you enqueue them below
orderQueue.Enqueue(new Order { OrderID = 16248, Customer = "Vins et alcools Chevalier", OrderDate = new DateTime(1997, 7, 4), ShipVia = "Federal Shipping" });
orderQueue.Enqueue(new Order { OrderID = 16249, Customer = "Toms Spezialitäten", OrderDate = new DateTime(1997, 7, 5), ShipVia = "Speedy Express" });
orderQueue.Enqueue(new Order { OrderID = 16250, Customer = "Hanari Carnes", OrderDate = new DateTime(1997, 7, 7), ShipVia = "United Package" });
orderQueue.Enqueue(new Order { OrderID = 16251, Customer = "Victuailles en stock", OrderDate = new DateTime(1997, 7, 8), ShipVia = "Speedy Express" });

PrintOrderQueue(orderQueue);

Console.WriteLine("Fetch the list and print all items from the List data structure...");

// Fetch the List data structure from the cache and then access all items in it
var fetchedProductList = cache.DataTypeManager.GetList<Product>("productList");

PrintProductList(fetchedProductList);

Console.WriteLine("Process all orders from the Queue one by one in a sequence\n");

// Fetch the Queue data structure from the cache and then process all orders one by one in a sequence
var fetchedOrderQueue = cache.DataTypeManager.GetQueue<Order>("orderQueue");
while (fetchedOrderQueue.Count > 0)
{
var order = orderQueue.Dequeue();
Console.WriteLine("Process order : " + order.OrderID);
PrintOrder(order);
}

// Removing all items from the list
productList.RemoveRange(0, productList.Count);

Console.WriteLine("Removed products from the List data structure.\n");

Console.WriteLine("Sample completed successfully.");
}

static void PrintProductList(IList<Product> productList)
{
Console.WriteLine("ProductID, Name, Price, Category, Tags");
foreach (var product in productList)
{
Console.WriteLine($"- {product.ProductID}, {product.Name}, ${product.Price}, {product.Category}");
}
Console.WriteLine();
}

static void PrintOrderQueue(IDistributedQueue<Order> orderQueue)
{
Console.WriteLine("OrderID, Customer, Order Date, Ship Via");
foreach (var order in orderQueue)
{
Console.WriteLine($"- {order.OrderID}, {order.Customer}, {order.OrderDate.ToShortDateString()}, {order.ShipVia}");
}
Console.WriteLine();
}

static void PrintOrder(Order order)
{
Console.WriteLine("OrderID, Customer, Order Date, Ship Via");
Console.WriteLine($"- {order.OrderID}, {order.Customer}, {order.OrderDate.ToShortDateString()}, {order.ShipVia} \n");
}
}

class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}

class Order
{
public int OrderID { get; set; }
public string Customer { get; set; }
public DateTime OrderDate { get; set; }
public string ShipVia { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"DataStructures": {
"commandName": "Project",
"commandLineArgs": "demoCache"
}
}
}
Loading