from System import Array

import clr
clr.AddReference("Microsoft.ApplicationServer.Caching.Client")
clr.AddReference("Microsoft.ApplicationServer.Caching.Core")

from Microsoft.ApplicationServer.Caching import DataCacheFactoryConfiguration, DataCache
from Microsoft.ApplicationServer.Caching import DataCacheServerEndpoint, DataCacheSecurity, DataCacheFactory
from System.Security import SecureString

from orangescape.model.server.azure.storageconfig import cacheHostName, cachePortNumber, cacheACSKey

def createCache():
    servers = Array[DataCacheServerEndpoint]([DataCacheServerEndpoint(cacheHostName, cachePortNumber)])
    secureACSKey = SecureString();
    for a in cacheACSKey:
        secureACSKey.AppendChar(a)
    secureACSKey.MakeReadOnly()
    factorySecurity = DataCacheSecurity(secureACSKey);

    factoryConfig = DataCacheFactoryConfiguration()

    factoryConfig.Servers = servers;
    factoryConfig.SecurityProperties = factorySecurity;
    cacheFactory = DataCacheFactory(factoryConfig);

    return cacheFactory.GetDefaultCache();

defaultCache = createCache()
