1

I am trying to save in cache response from server for certain time.

There are tne next data for saving in cache: I have a List<ProgrammeItem> which I am getting from server. While user is working, he can download up to ~230 List<ProgrammeItem> (but it is unreal to reach this, estimated is 10-50). ProgrammeItem oblect including strings, int, int[].

That is how I am saving and getting the last downloaded List<ProgrammeItem>:

//saving / getting Programme items
public boolean saveObject(List<ProgrammeItem> obj) {
    final File suspend_f=new File(android.os.Environment.getExternalStorageDirectory(), "test");

    FileOutputStream   fos  = null;
    ObjectOutputStream oos  = null;
    boolean            keep = true;

    try {
        fos = new FileOutputStream(suspend_f);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (Exception e) {
        keep = false;
        Log.e("catching exception ", "" + e.getMessage() + ";;;" + e);

    } finally {
        try {
            if (oos != null)   oos.close();
            if (fos != null)   fos.close();
            if (keep == false) suspend_f.delete();
        } catch (Exception e) { /* do nothing */ }
    }

    return keep;
}

public List<ProgrammeItem> getObject(Context c) {
    final File suspend_f=new File(android.os.Environment.getExternalStorageDirectory(), "test");

    List<ProgrammeItem> simpleClass= null;
    FileInputStream fis = null;
    ObjectInputStream is = null;

    try {
        fis = new FileInputStream(suspend_f);
        is = new ObjectInputStream(fis);
        simpleClass = (List<ProgrammeItem>) is.readObject();
    } catch(Exception e) {
        String val= e.getMessage();
    } finally {
        try {
            if (fis != null)   fis.close();
            if (is != null)   is.close();
        } catch (Exception e) { }
    }

    return simpleClass;
}

That is how I am saving and getting object in activity:

PI = new ProgrammeItem();
List<ProgrammeItem> programmeItems = new ArrayList<>();
...
//filling programmeItems with data from server
...
boolean result = PI.saveObject(programmeItems); //Save object
ProgrammeItem m = new ProgrammeItem();
List<ProgrammeItem> c = m.getObject(getApplicationContext()); //Get object

The question is: how can I save a lot of my objects instead of only one?

I think I should done something like public boolean addObjectsInCache(List<ProgrammeItem> obj) for adding objects, not overriding them.

And change get method into public List<ProgrammeItem> getObject(Context c, String id), where id will be unique identifier, which will includes into every ProgrammeItem in the every List<ProgrammeItem>.

Am I right? And how I can achieve this? Maybe you will show me the other way to work with objects and cache?

danyapd
  • 2,516
  • 1
  • 14
  • 23

2 Answers2

1

You can use SharedPreference instead, while having a local database Android Room can also be an option. SharedPreference basically is stored in your device's cache while the local database is stored in your device's data hence in our apps we have clear cache and clear data function.

Additional Resources:

Mike
  • 1,313
  • 12
  • 21
  • 1) I am using SharedPreferences for my other needs (strings etc), I will read this one, thank you; 2) Is this acceptable and safe to use this library (PreferencesManager) in commercial project? 3) or you are saying to encrypt data with Secured Preferences which I will saving with PreferencesManager? – danyapd Mar 31 '20 at 09:14
  • If you think the data will be very sensitive I suggest you use Secured Preferences + GSON if the data is simple and doesn't need querying however if you think it will be ideal to use a database, a secure alternative is https://github.com/square/sqlbrite with the cipher integration to make it secure e.g. https://github.com/vexdev/sqlcipherbrite also with regards to SharedPrefs you can have as many as you want and you can also secure them using android keystore https://developer.android.com/training/articles/keystore – Mike Mar 31 '20 at 09:18
  • 1
    I've choose variant with SharedPrefs and Gson - it's using internal android tools and simple in realization as those libraries. I've right more code than in those guide, but not a lot - just because I have not a standard json string. It Is working perfect now and I didn't see any minuses of this method. thanks for help. – danyapd Apr 01 '20 at 08:05
1

An option could be to use Room database with inMemoryDatabaseBuilder:

db = Room.inMemoryDatabaseBuilder(context, ProgrammeDatabase::class.java)
                .build()

if it all can fit in memory.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • Yes, there are max 40Kb per each `List<>` item, I think it fits. @Mike suggested Room database too, so I will try this and will respond to you. – danyapd Mar 31 '20 at 09:56