{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-docs/functions/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":["partial"]},"type":"markdown"},"seo":{"title":"Developer Documentation","siteUrl":"https://developers.sinch.com","llmstxt":{"title":"Sinch Developer Documentation","description":"LLMs.txt containing a map of all the documentation files for Sinch.","sections":[{"title":"SMS API","description":"The SMS API allows you to send and receive SMS messages with a few easy steps. You can also send bulk SMS messages to multiple customers using the Sinch SMS service.","includeFiles":["docs/sms/**/*.md","docs/sms/**/*.yaml"],"excludeFiles":["docs/sms/index.md"]},{"title":"Numbers API","description":"The Numbers API enables you to search for, view, and activate numbers. It's considered a precursor to other APIs in the Sinch product family. The numbers API can be used in tandem with any of our APIs that perform messaging or calling.","includeFiles":["docs/numbers/**/*.md","docs/numbers/**/*.yaml"],"excludeFiles":["docs/numbers/index.md"]},{"title":"Conversation API","description":"Send and receive messages globally on many popular channels with ease and confidence when using Sinch's Conversation API. Conversation API is the preferred API for sending mobile messages on SMS and other social channels with Sinch. It is a simple API with unified error messages, consistent request payloads, and common webhook payloads that are channel-agnostic.","includeFiles":["docs/conversation/**/*.md","docs/conversation/**/*.yaml"],"excludeFiles":["docs/conversation/index.md"]},{"title":"Voice API","description":"The Voice API works as a big telephony switch. The Voice API handles incoming phone calls (also known as incoming call “legs”), sets up outgoing phone calls (or outgoing call “legs”), and bridges the two. The incoming call leg may come in over a data connection (from a smartphone or web application using the Sinch SDKs) or through a local phone number (from the PSTN network). Similarly, the outgoing call leg can be over data (to another smartphone or web application using the Sinch SDKs) or the PSTN network.","includeFiles":["docs/voice/**/*.md","docs/voice/**/*.yaml"],"excludeFiles":["docs/voice/index.md"]},{"title":"Verification API","description":"The Verification API is a platform for phone number verification. It consists of the API and different software development kits (the Sinch SDKs) that you integrate with your smartphone or web application and cloud based back-end services. Together they enable SMS, Flashcall, Phone Call and Data verification in your application.","includeFiles":["docs/verification/**/*.md","docs/verification/**/*.yaml"],"excludeFiles":["docs/verification/index.md"]},{"title":"Provisioning API","description":"Provisioning API allows you to programmatically set up your senders, accounts and templates on your favorite messaging platforms on the Conversation API. For now, you can create your first WhatsApp channel through Meta's Embedded sign up, you can configure your first SMS App and configure your webhooks. As development continues, we will be adding the most commonly used channels.","includeFiles":["docs/provisioning-api/**/*.md","docs/provisioning-api/**/*.json"],"excludeFiles":["docs/provisioning-api/index.md"]},{"title":"Elastic SIP Trunking API","description":"With Elastic SIP Trunking you can create and manage your SIP trunks and phone numbers programmatically.","includeFiles":["docs/est/**/*.md","docs/est/**/*.yaml"],"excludeFiles":["docs/est/index.md"]},{"title":"Fax API","description":"Send and receive HIPAA compliant faxes on our modern fax platform using our developer-friendly API.","includeFiles":["docs/fax/**/*.md","docs/fax/**/*.yaml"],"excludeFiles":["docs/fax/index.md"]},{"title":"In-app Voice and Video SDK","description":"The In-app Voice and Video SDK enables you to add voice and video calling capabilities directly into your mobile or web application using the Sinch SDKs.","includeFiles":["docs/in-app-calling/**/*.md"],"excludeFiles":["docs/in-app-calling/index.md"]}],"hide":false,"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"working-with-the-cache","__idx":0},"children":["Working with the Cache"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Every Sinch Function has access to a shared key-value cache. In development it uses in-memory storage. In production, the cache is persistent across restarts and shared across all instances of your function."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"interface","__idx":1},"children":["Interface"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Operation"},"children":["Operation"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Node.js"},"children":["Node.js"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"C#"},"children":["C#"]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Write"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.set(key, value, ttl?)"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.Set(key, value, ttl?)"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Read"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.get<T>(key)"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.Get<T>(key)"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Check existence"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.has(key)"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.Exists(key)"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Delete"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.delete(key)"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.Delete(key)"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Extend TTL"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.extend(key, seconds)"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["—"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["List keys"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.keys(pattern?)"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.GetKeys()"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Batch read"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cache.getMany(keys)"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["—"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Default TTL: ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["3600 seconds"]}," (1 hour)."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"accessing-the-cache","__idx":2},"children":["Accessing the cache"]},{"$$mdtype":"Tag","name":"Tabs","attributes":{"size":"medium"},"children":[{"$$mdtype":"Tag","name":"div","attributes":{"label":"Node.js","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"typescript","header":{"controls":{"copy":{}}},"source":"import type { FunctionContext, FunctionRequest } from '@sinch/functions-runtime';\n\nexport async function myHandler(context: FunctionContext, request: FunctionRequest) {\n  const cache = context.cache;\n  await cache.set('greeting', 'Hello!');\n  const value = await cache.get<string>('greeting'); // 'Hello!'\n}\n","lang":"typescript"},"children":[]}]},{"$$mdtype":"Tag","name":"div","attributes":{"label":"C#","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"csharp","header":{"controls":{"copy":{}}},"source":"public class FunctionController : SinchController\n{\n    public async Task<IActionResult> MyEndpoint()\n    {\n        var cache = Context.Cache;\n        await cache.Set(\"greeting\", \"Hello!\");\n        var value = await cache.Get(\"greeting\");\n        return Ok(value);\n    }\n}\n","lang":"csharp"},"children":[]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"ttl-time-to-live","__idx":3},"children":["TTL (time to live)"]},{"$$mdtype":"Tag","name":"Tabs","attributes":{"size":"medium"},"children":[{"$$mdtype":"Tag","name":"div","attributes":{"label":"Node.js","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"typescript","header":{"controls":{"copy":{}}},"source":"await cache.set('short-lived', 'value', 60); // 60 seconds\nawait cache.set('persistent', 'value', 86400 * 30); // 30 days\nawait cache.extend('session:abc123', 300); // extend by 5 minutes\n","lang":"typescript"},"children":[]}]},{"$$mdtype":"Tag","name":"div","attributes":{"label":"C#","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"csharp","header":{"controls":{"copy":{}}},"source":"await cache.Set(\"short-lived\", \"value\", ttlSeconds: 60);\nawait cache.Set(\"persistent\", \"value\", ttlSeconds: 86400 * 30);\n","lang":"csharp"},"children":[]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"storing-objects","__idx":4},"children":["Storing objects"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Values are JSON-serialized automatically."]},{"$$mdtype":"Tag","name":"Tabs","attributes":{"size":"medium"},"children":[{"$$mdtype":"Tag","name":"div","attributes":{"label":"Node.js","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"typescript","header":{"controls":{"copy":{}}},"source":"interface UserProfile {\n  name: string;\n  plan: string;\n  createdAt: string;\n}\n\nawait cache.set<UserProfile>(\n  'user:123',\n  {\n    name: 'Alice',\n    plan: 'pro',\n    createdAt: new Date().toISOString(),\n  },\n  3600\n);\n\nconst profile = await cache.get<UserProfile>('user:123');\n","lang":"typescript"},"children":[]}]},{"$$mdtype":"Tag","name":"div","attributes":{"label":"C#","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"csharp","header":{"controls":{"copy":{}}},"source":"public record UserProfile(string Name, string Plan, DateTime CreatedAt);\n\nawait cache.Set(\"user:123\", new UserProfile(\"Alice\", \"pro\", DateTime.UtcNow), ttlSeconds: 3600);\nvar profile = await cache.Get<UserProfile>(\"user:123\");\n","lang":"csharp"},"children":[]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"common-patterns","__idx":5},"children":["Common patterns"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"session-storage","__idx":6},"children":["Session storage"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Store caller state across voice callbacks using the call ID as key."]},{"$$mdtype":"Tag","name":"Tabs","attributes":{"size":"medium"},"children":[{"$$mdtype":"Tag","name":"div","attributes":{"label":"Node.js","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"typescript","header":{"controls":{"copy":{}}},"source":"// In ICE handler\nawait context.cache.set(\n  `session:${event.callId}`,\n  {\n    callerNumber: event.cli,\n    startTime: Date.now(),\n  },\n  600\n);\n\n// In PIE handler\nconst session = await context.cache.get<{ callerNumber: string; startTime: number }>(\n  `session:${event.callId}`\n);\n","lang":"typescript"},"children":[]}]},{"$$mdtype":"Tag","name":"div","attributes":{"label":"C#","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"csharp","header":{"controls":{"copy":{}}},"source":"// In ICE handler\nawait Context.Cache.Set($\"session:{callbackData.CallId}\", new\n{\n    CallerNumber = callbackData.Cli, StartTime = DateTime.UtcNow\n}, ttlSeconds: 600);\n\n// In PIE handler\nvar session = await Context.Cache.Get<dynamic>($\"session:{callbackData.CallId}\");\n","lang":"csharp"},"children":[]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"rate-limiting","__idx":7},"children":["Rate limiting"]},{"$$mdtype":"Tag","name":"Tabs","attributes":{"size":"medium"},"children":[{"$$mdtype":"Tag","name":"div","attributes":{"label":"Node.js","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"typescript","header":{"controls":{"copy":{}}},"source":"async function isRateLimited(\n  cache: IFunctionCache,\n  caller: string,\n  maxPerHour = 10\n): Promise<boolean> {\n  const key = `rate:${caller}`;\n  const current = await cache.get<number>(key);\n  if (current === null) {\n    await cache.set(key, 1, 3600);\n    return false;\n  }\n  if (current >= maxPerHour) return true;\n  await cache.set(key, current + 1, 3600);\n  return false;\n}\n","lang":"typescript"},"children":[]}]},{"$$mdtype":"Tag","name":"div","attributes":{"label":"C#","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"csharp","header":{"controls":{"copy":{}}},"source":"private async Task<bool> IsRateLimited(string caller, int maxPerHour = 10)\n{\n    var key = $\"rate:{caller}\";\n    var current = await Context.Cache.Get(key);\n    if (current == null) { await Context.Cache.Set(key, \"1\", ttlSeconds: 3600); return false; }\n    var count = int.Parse(current);\n    if (count >= maxPerHour) return true;\n    await Context.Cache.Set(key, (count + 1).ToString(), ttlSeconds: 3600);\n    return false;\n}\n","lang":"csharp"},"children":[]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"caller-lookup-cache","__idx":8},"children":["Caller lookup cache"]},{"$$mdtype":"Tag","name":"Tabs","attributes":{"size":"medium"},"children":[{"$$mdtype":"Tag","name":"div","attributes":{"label":"Node.js","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"typescript","header":{"controls":{"copy":{}}},"source":"async function getCustomer(cache: IFunctionCache, phone: string) {\n  const cacheKey = `customer:${phone}`;\n  const cached = await cache.get<Customer>(cacheKey);\n  if (cached) return cached;\n\n  const customer = await fetchCustomerFromApi(phone);\n  if (customer) await cache.set(cacheKey, customer, 300);\n  return customer;\n}\n","lang":"typescript"},"children":[]}]},{"$$mdtype":"Tag","name":"div","attributes":{"label":"C#","disable":false},"children":[{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"csharp","header":{"controls":{"copy":{}}},"source":"private async Task<Customer?> GetCustomer(string phone)\n{\n    var cacheKey = $\"customer:{phone}\";\n    var cached = await Context.Cache.Get<Customer>(cacheKey);\n    if (cached != null) return cached;\n\n    var customer = await FetchCustomerFromApi(phone);\n    if (customer != null) await Context.Cache.Set(cacheKey, customer, ttlSeconds: 300);\n    return customer;\n}\n","lang":"csharp"},"children":[]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"development-vs-production","__idx":9},"children":["Development vs production"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Aspect"},"children":["Aspect"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Development"},"children":["Development"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Production"},"children":["Production"]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Backend"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["In-memory"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Persistent store"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Persistence"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Lost on restart"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Survives restarts"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Sharing"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Single process only"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Shared across all instances"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["TTL enforcement"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Approximate"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Exact"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["IFunctionCache"]}," interface is identical in both environments. No code changes needed when deploying."]}]},"headings":[{"value":"Working with the Cache","id":"working-with-the-cache","depth":1},{"value":"Interface","id":"interface","depth":2},{"value":"Accessing the cache","id":"accessing-the-cache","depth":2},{"value":"TTL (time to live)","id":"ttl-time-to-live","depth":2},{"value":"Storing objects","id":"storing-objects","depth":2},{"value":"Common patterns","id":"common-patterns","depth":2},{"value":"Session storage","id":"session-storage","depth":3},{"value":"Rate limiting","id":"rate-limiting","depth":3},{"value":"Caller lookup cache","id":"caller-lookup-cache","depth":3},{"value":"Development vs production","id":"development-vs-production","depth":2}],"frontmatter":{"seo":{"title":""}},"lastModified":"2026-04-15T14:23:23.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/docs/functions/guides/use-the-cache","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}