Android Simple And Powerful Cache Manager

Android Simple And Powerful Cache Manager

package com.fisher.utils;


import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.io.File;
import java.io.IOException;

/**
 * Created on 6/12/2016 at 1:08
 * By Fisher
 * used to save and fetch caches
 */
public class CacheManagerUtil< T > {
    public static final String CACHE_DIR = "data";

    public static < T > CacheManagerUtil< T > getInstance ( T data ) {
        return new CacheManagerUtil<>( data );
    }

    public void save ( ) {
        try {
            String name = data.getClass().getPackage() + "." + data.getClass().getName();
            FileUtil.writeFile( getFile(name), this.toString() );
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    public static < T > CacheManagerUtil< T > getCache ( Class< T > cls ) {
        String name = cls.getPackage() + "." + cls.getName();
        CacheManagerUtil< T > data = null;
        try {
            String json = FileUtil.readFile( FileUtil.getInternalFile( "data" + FileUtil.separator + TextUtil.md5( name ) ) );
            if ( null == json || "".equals( json ) ) {
                return null;
            } else {
                try {
                    data = new Gson().fromJson( json, CacheManagerUtil.class );
                } catch ( JsonSyntaxException e ) {
                    e.printStackTrace();
                }
            }
        } catch ( IOException e ) {
            e.printStackTrace();
        }
        return data;
    }

    public static File getFile (String name ) {
        return FileUtil.getInternalFile( CACHE_DIR + FileUtil.separator + TextUtil.md5( name ) );
    }


    private CacheManagerUtil ( T data ) {
        this.data = data;
    }

    private long ctime;
    private T data;

    public long getCtime ( ) {
        return ctime;
    }

    public void setCtime ( long ctime ) {
        this.ctime = ctime;
    }

    public T getData ( ) {
        return data;
    }

    public void setData ( T data ) {
        this.ctime = System.currentTimeMillis();
        this.data = data;
    }
    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}

as you can see here

have fun with it :-)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NestJS Cache Manager is a module for caching data in NestJS applications. It provides a simple interface for caching data using various caching strategies such as memory, Redis, and others. Redis is one of the caching strategies that NestJS Cache Manager supports. Redis is an in-memory data structure store that can be used as a database, cache, and message broker. It is often used as a cache because of its high performance and scalability. Redis stores data in memory, which makes it faster than traditional disk-based databases. Using Redis as a caching strategy in NestJS Cache Manager is easy. First, you need to install the Redis module: ``` npm install cache-manager-redis-store ``` Next, you need to configure the Redis cache in your NestJS application: ``` import { CacheModule, Module } from '@nestjs/common'; import * as redisStore from 'cache-manager-redis-store'; @Module({ imports: [ CacheModule.register({ store: redisStore, host: 'localhost', port: 6379, }), ], }) export class AppModule {} ``` In this example, we import the CacheModule and configure it to use the Redis store. We set the host and port to connect to the Redis instance. Now, you can use the cache in your NestJS application: ``` import { CacheInterceptor, CacheTTL, Controller, Get, UseInterceptors } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get() @UseInterceptors(CacheInterceptor) @CacheTTL(60) findAll(): string[] { return ['Cat 1', 'Cat 2', 'Cat 3']; } } ``` In this example, we use the CacheInterceptor to cache the response of the findAll() method for 60 seconds. This means that subsequent requests for the same resource will be served from the cache, which improves performance and reduces the load on the server. Overall, using Redis as a caching strategy in NestJS Cache Manager is a great way to improve the performance and scalability of your NestJS application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值