Would it be wrong to use a static object instead of a database?

This is essentially a question of design patterns:

I was expecting a database query to get a list of the stocks (stocks / securities) that are most strongly correlated for a given stock.

Instead, I thought, maybe I should create an object that has a static HashMap and store my data there. Then "query" this every time I need.

Will there be something wrong with this approach, since I believe that this will significantly improve performance compared to querying the database for the same data. The amount of data is relatively small and does not grow, so this will not cause a problem. Could there be any problems that bite me later?

+5
source share
11 answers

I would still use the database for backup reasons, but on the client use api caching, such as oscache, to store data in the local file system for quick access, and then if the system will recover the cache from the database and transfer when using the cache in code

+4
source

If read-only cache access that does not update automatically is all you need for your application, then there is nothing wrong with this approach.

: ( Java HashMap) , , ( 10 000 ), ; HashMap .

+3

. , , " bad singleton". ( )

- , . " ", .

public class MyDataClass {
    private MyDataClass() { }
    public static MyDataClass getInstance() {
        if (instance == null) instance = new MyDataClass();
        return instance;
    }
    private static MyDataClass instance = null;
}

public class MyDataProcessor {
    public void registerData(MyDataClass data) { 
       this.data = data;
    }
    public void process() {
        assert(data != null);
        data.getData(...);
    }
    private MyDataClass data;
}

, , , ,... , , .

+1

.

:

  • / .

, -, BerkeleyDB, .

+1

, , - , .

, , , .

, , HashMap.

+1

, , - , - . , , , , , , . , . 2c.

+1

, .

, , / , , , , . , .

+1

HashMap, .

, XStream. XML- HashMap.

, , . , HashMap , .

0

.

, , , -

0

? , , , , .

API . , .

0

, , , . , - , HashMap .. / .

, , . , XStream .

There is also a library called Prevayler . This serializes the changes on the fly, but allows you to store everything in memory. Thus, even in the event of a sudden power failure, when you do not have time to shut down correctly, this can be useful.

Experiment with various methods to see what works for you, and extract the details behind the DAO.

0
source

All Articles