
module(..., package.seeall)

function Cache(fetchFunction, timeout)
	timeout = timeout or 30
	local cache = {}
	
	local get = function(key, ...)
		key = key or ""
		local entry = cache[key]
		if entry and (entry.timestamp + timeout > os.time()) then
			return entry.data
		else
			local data = fetchFunction(key, ...)
			if data ~= nil then
				cache[key] = {
					data = data,
					timestamp = os.time()
				}
			end
			return data
		end
	end

	return {
		get = get
	}
end

function get_user_session_type(authUser)
	return authUser.sessionData and authUser.sessionData.type or "WEB"
end

function get_session_time_key(session_type, vcc)
	return "presencefilter:auth_tokens:" .. session_type .. ":" .. vcc
end

function get_user_session_time_key(authUser)
	return get_session_time_key(get_user_session_type(authUser), authUser.virtualContactCenter)
end
