Android sharedPreference设置缓存时间
生活随笔
收集整理的這篇文章主要介紹了
Android sharedPreference设置缓存时间
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
不廢話,需求:緩存登錄時的token,超過設(shè)置的存儲時間就無效,怎么做?
使用ACache也可以設(shè)置緩存時間,但ACache在清緩存的時候會被清空。?
SharedPreferences存儲默認都是無時間限制的。?
大概思路是,存儲的時候記錄當(dāng)前時間,要存多久。取數(shù)據(jù)的時候判斷這個數(shù)據(jù)已經(jīng)存儲了多久,如果超過設(shè)置的存儲時間,就獲取默認值。?
首先,我們需要一個存儲的model——SpSaveModel
?
需要一個object和json字符串轉(zhuǎn)換的工具,這里用fastJson,添加依賴
compile 'com.alibaba:fastjson:1.1.26' import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.TextUtils;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference;/*** Created by KID on 2018/5/3.*/ public class SpUtils {//保存時間單位public static final int TIME_SECOND=1;public static final int TIME_MINUTES=60*TIME_SECOND;public static final int TIME_HOUR = 60 *TIME_MINUTES;public static final int TIME_DAY = TIME_HOUR * 24;public static final int TIME_MAX = Integer.MAX_VALUE; // 不限制存放數(shù)據(jù)的數(shù)量public static final int DURATION_UNIT=1000;private static final String fileName = "config";private SharedPreferences sp;private Editor editor;private static SpUtils INSTANCE = null;public static SpUtils getInstance(Context context) {if (null == INSTANCE) {INSTANCE = new SpUtils(context);}return INSTANCE;}private SpUtils(Context context) {sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);editor = sp.edit();}public void setString(String e, String value) {SpSaveModel<String>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}/**** @param e 存放的key* @param value 存放的value* @param saveTime 緩存時間*/public void setString(String e, String value,int saveTime) {SpSaveModel<String>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}/**** @param e 存放的key* @param defValue 該key不存在或者過期時,返回的默認值* @return*/public String getString(String e, String defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<String>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<String>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public void setInt(String e, int value) {SpSaveModel<Integer>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public void setInt(String e, int value,int saveTime) {SpSaveModel<Integer>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public Integer getInt(String e, int defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<Integer>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<Integer>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public void setBoolean(String e, boolean value) {SpSaveModel<Boolean>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public void setBoolean(String e, boolean value,int saveTime) {SpSaveModel<Boolean>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public boolean getBoolean(String e, boolean defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<Boolean>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<Boolean>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public void setLong(String e, long value) {SpSaveModel<Long>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public void setLong(String e, long value,int saveTime) {SpSaveModel<Long>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public long getLong(String e, long defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<Long>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<Long>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public boolean isTimeOut(long saveCurrentTime,int saveTime){return (System.currentTimeMillis()-saveCurrentTime)/DURATION_UNIT<saveTime;}public void set(String e, Object value,int saveTime) {SpSaveModel<Object>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}}使用
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {save();}});findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {get();}});}//保存private void save() {//保存10秒 // SpUtils.getInstance(this).set("text","我的世界",10);SpUtils.getInstance(this).setString("text","我的世界",10);SpUtils.getInstance(this).setInt("num",123456,10);SpUtils.getInstance(this).setBoolean("isBu",true,10);}//獲取private void get() {String text=SpUtils.getInstance(this).getString("text","超時了");int num=SpUtils.getInstance(this).getInt("num",-100);boolean isBu=SpUtils.getInstance(this).getBoolean("isBu",false);Log.e("kid","text======="+text);Log.e("kid","num======="+num);Log.e("kid","isBu======="+isBu);}}PS:存的時候,不加存儲時間,默認是永久
參考文章:https://blog.csdn.net/qq_31390699/article/details/80182661? 寫得很好,我直接用的
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Android sharedPreference设置缓存时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 通过高德地图获取地址的经
- 下一篇: Android edittext限制字节