Пример #1
0
        public void AddOrUpdate <T>(object key, T valor)
        {
            //TODO ESTADO ACTIVO
            if (IsActiveCache())
            {
                //_peticiones++;
                // AddPeticion();

                try
                {
                    //CachedItem it = diccionario.GetOrAdd(new CacheKey(key), (copyOfMyKey) =>
                    //{
                    //    //handler se ejecuta por que la llave no existe
                    //    //se llama al constructor por defecto
                    //    //objeto es nuevo
                    //    return new CachedItem(this);
                    //});
                    CachedItem it = new CachedItem(this);
                    it.Value = valor;
                    it.Fecha = DateTime.Now;

                    //opcion hacerlo paralelo  no generar bloqueo
                    //  Parallel.Invoke(() =>
                    //{
                    //
                    diccionario.AddOrUpdate(new CacheKey(key), it, (k, existingVal) =>
                    {
                        //
                        existingVal.Fecha = it.Fecha;
                        existingVal.Value = it.Value;
                        return(existingVal);
                    });
                    //

                    //  });
                    //
                }
                catch (Exception ex)
                {
                    throw;
                }
                finally
                {
                    //_peticiones--;
                    //QuitPeticion();
                    //if (_peticiones < 0)
                    //    logger.ErrorHigh("[FATAL] ESTADO INCOHERENTE -1");
                }
            }
        }
Пример #2
0
        public bool Add <T>(object key, T valor)
        {
            try
            {
                // _peticiones++;
                //AddPeticion();

                CachedItem cache = new CachedItem(this);
                cache.Fecha = DateTime.Now;
                cache.Value = valor;

                return(diccionario.TryAdd(new CacheKey(key), cache));
            }
            finally
            {
                // _peticiones--;
                // QuitPeticion();
            }
        }
Пример #3
0
        public S GetValue <T, S>(object key, T param, Func <T, S> func, TimeSpan time)
        {
            //TODO ESTADO ACTIVO
            //atendiendo peticion
            //atiendo peticion
            bool activeCache = IsActiveCache();

            // if(DEBUG_FIND)
            // logger.InfoHigh(String.Concat("cache activo ", activeCache, " peticiones ", _peticiones));



            if (activeCache)
            {
                try
                {
                    #region Implementacion concreta para obtener un valor del cache
                    var        fecha = DateTime.Now;
                    CachedItem it    = diccionario.GetOrAdd(new CacheKey(key), (copyOfMyKey) =>
                    {
                        //handler se ejecuta por que la llave no existe
                        //se llama al constructor por defecto
                        //objeto es nuevo
                        return(new CachedItem(this));
                    });

                    //si el objeto es nuevo o la fecha expiro
                    int state = it.State(time);
                    if (state != CachedItem.CacheValido)//(it.Value == null || (fecha - it.Fecha) > time)
                    {
                        //logger.InfoHigh(String.Concat(key, " VALUE FROM HANDLER", state));
                        S result = func(param);
                        it.Fecha = fecha;
                        it.Value = result;
                        // se agrega o se actualiza
                        diccionario.AddOrUpdate(new CacheKey(key), it, (k, existingVal) =>
                        {
                            //
                            existingVal.Fecha = it.Fecha;
                            existingVal.Value = it.Value;
                            return(existingVal);
                        });
                        return(result);
                    }
                    else
                    {
                        //logger.InfoHigh(String.Concat(key, " VALUE FROM CACHE", state));
                        S result = (S)it.Value;

                        return(result);
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    throw;
                }
                finally
                {
                    //_peticiones--;
                    //if (_peticiones < 0)
                    //    logger.ErrorHigh("[FATAL] ESTADO INCOHERENTE -1");
                }
            }
            else
            {
                return(func(param));
            }
        }
Пример #4
0
        /// <summary>
        /// Obtengo un objeto en cache si el objeto es nuevo
        /// o el tiempo de cache se cumplio se ejecuta el delegado
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="func">delegado para ejecutar </param>
        /// <param name="time"></param>
        /// <param name="oncache">Call back a ejecutar si el objeto esta en cache</param>
        /// <returns></returns>
        public T GetValue <T>(object key, Func <T> func, TimeSpan time, Action <Object, Object> oncache)
        {
            //atiendo peticion
            bool activeCache = IsActiveCache();

            //TODO TEST
            //if (DEBUG_FIND)
            //    logger.InfoHigh(String.Concat("cache activo ", activeCache, " peticiones ", _peticiones));

            if (activeCache)
            {
                //TODO FASE DE PRUEBAS
                //_peticiones++;
                //AddPeticion();
                try
                {
                    #region Implementacion concreta para obtener un valor del cache


                    var fecha = DateTime.Now;


                    CachedItem it = diccionario.GetOrAdd(new CacheKey(key), (copyOfMyKey) =>
                    {
                        //handler se ejecuta por que la llave no existe
                        //se llama al constructor por defecto
                        //objeto es nuevo
                        return(new CachedItem(this));
                    });

                    //si el objeto es nuevo o la fecha expiro
                    int state = it.State(time);
                    if (state != CachedItem.CacheValido)//it.Value == null || (fecha - it.Fecha) > time)
                    {
                        //logger.InfoHigh(String.Concat(key, " VALUE FROM HANDLER ", state));

                        T result = func();
                        it.Fecha = fecha;
                        it.Value = result;
                        // se agrega o se actualiza
                        diccionario.AddOrUpdate(new CacheKey(key), it, (k, existingVal) =>
                        {
                            //
                            existingVal.Fecha = it.Fecha;
                            existingVal.Value = it.Value;
                            return(existingVal);
                        });

                        return(result);
                    }
                    else
                    {
                        //logger.InfoHigh(String.Concat(key, " VALUE FROM CACHE ", state));
                        //oncache

                        T result = (T)it.Value;

                        if (oncache != null)
                        {
                            oncache(key, result);
                        }



                        return(result);
                    }


                    #endregion
                }
                catch (Exception ex)
                {
                    throw;
                }
                finally
                {
                    //TODO FASE DE PRUEBAS
                    //QuitPeticion();
                }
            }
            else
            {
                return(func());
            }
        }