Ejemplo n.º 1
0
func (application Application) FindEndpointByName(name string) Endpoint {
	result := Endpoint{}

	key := application.Id.Hex() + ".endpoints." + name
	cache_result, err := cache.Get(key)

	if err {
		query := bson.M{"name": name}
		database.FindBy(application.Id.Hex()+"_endpoints", &result, query)

		b, _ := json.Marshal(result)
		cache.Set(key, string(b), 5*time.Minute)

		// Set a second cache item for the endpoint id (This stores reference map data)
		key_id := application.Id.Hex() + ".endpoints." + result.Id.Hex()
		cache.Set(key_id, string(b), 5*time.Minute)
	} else if cache_result.CachedItem == nil {
		json.Unmarshal([]byte(cache_result.Value.(string)), &result)
		cache_result.CachedItem = result
	} else {
		result = cache_result.CachedItem.(Endpoint)
	}

	return result
}
Ejemplo n.º 2
0
func findTriggerForApplicationIdAndEvent(application_id_bson bson.ObjectId, event string) Trigger {
	application_id := application_id_bson.Hex()

	key := application_id + ".triggers." + event
	result := Trigger{}

	cache_result, err := cache.Get(key)

	if err {
		query := bson.M{"event": event, "type": "trigger"}

		collection := application_id
		collection += "_triggers"

		database.FindBy(collection, &result, query)

		b, _ := json.Marshal(result)
		cache.Set(key, string(b), 5*time.Minute)
	} else if cache_result.CachedItem == nil {
		json.Unmarshal([]byte(cache_result.Value.(string)), &result)
		cache_result.CachedItem = result
	} else {
		result = cache_result.CachedItem.(Trigger)
	}
	return result
}
Ejemplo n.º 3
0
func (r *Record) GetDependantReferenceMaps(connection *database.Connection) []ReferenceMap {

	endpoint_id := r.EndpointId
	application_id := r.ApplicationId

	endpoint := r.GetEndpointFromCache()

	var results []ReferenceMap

	if endpoint.CachedSourceReferenceMaps {
		logger.Debug("[Cache][Reference Maps][Source] Hit: ", r.EndpointId.Hex())
		results = endpoint.SourceReferencedMaps
	} else {
		logger.Debug("[Cache][Reference Maps][Source] Miss: ", r.EndpointId.Hex())
		query := map[string]bson.ObjectId{"source_id": endpoint_id}

		// Get the collection name
		collection := application_id.Hex() + "_maps"

		// Query the database
		database.Query(connection, collection, query).All(&results)

		if endpoint.Id != bson.ObjectId("") {
			key := r.ApplicationId.Hex() + ".endpoints." + endpoint_id.Hex()
			endpoint.SourceReferencedMaps = results
			endpoint.CachedSourceReferenceMaps = true
			b, _ := json.Marshal(endpoint)
			cache.Set(key, string(b), 5*time.Minute)

			logger.Debug("[Cache][Reference Maps][Source] Set: ", r.EndpointId.Hex())
		}
	}

	return results
}
Ejemplo n.º 4
0
func (object *Session) Save() {

	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Saving Session")

	collection := connection.C(object.Collection())
	if object.Id == bson.ObjectId("") {
		object.Id = bson.NewObjectId()
		collection.Insert(object)
	} else {
		collection.UpdateId(object.Id, object)
	}

	b, _ := json.Marshal(object)
	key := object.ApplicationId.Hex() + ".sessions." + object.Id.Hex()
	cache.Set(key, string(b), 5*time.Minute)
}
Ejemplo n.º 5
0
func FindApplicationById(id bson.ObjectId) Application {
	result := Application{}

	key := id.Hex()
	cache_result, err := cache.Get(key)

	if err {
		database.Find(result.Collection(), &result, id)
		b, _ := json.Marshal(result)
		cache.Set(key, string(b), 60*time.Minute)
	} else if cache_result.CachedItem == nil {
		json.Unmarshal([]byte(cache_result.Value.(string)), &result)
		cache_result.CachedItem = result
	} else {
		result = cache_result.CachedItem.(Application)
	}

	return result
}
Ejemplo n.º 6
0
func (application Application) FindSessionById(id bson.ObjectId) (Session, bool) {
	result := Session{}

	key := application.Id.Hex() + ".sessions." + id.Hex()
	cache_result, err := cache.Get(key)

	if err {
		query := bson.M{"_id": bson.ObjectId(id)}
		database.FindBy(application.Id.Hex()+"_sessions", &result, query)

		b, _ := json.Marshal(result)
		cache.Set(key, string(b), 5*time.Minute)
	} else if cache_result.CachedItem == nil {
		json.Unmarshal([]byte(cache_result.Value.(string)), &result)
		cache_result.CachedItem = result
	} else {
		result = cache_result.CachedItem.(Session)
	}

	return result, result.Id == bson.ObjectId("")
}