PHP-静态类和缓存类 [英] PHP - static classes and caching classes

查看:82
本文介绍了PHP-静态类和缓存类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是我用来在 Java 中缓存对象的方式.

The code below is how I used to cache objects in Java.

class Account{
    private static ArrayList<Account> accounts = new ArrayList<Account>(); //Array that holds Account objects
    private String username; //Unique Name or Username

    public Account(String username) { // constructor
        this.username = username;
        Account.accounts.add(this); //Add object to accounts Array
    }

    public String getUsername() { 
        return this.username; // Return username
    }

    public Account getAccount(String username) { //Get object "Account" with username
        for (Account acc: Account.accounts) { //Foreach loop that loop over all accounts from the Array
            if (acc.getUsername() == username) return acc; // Return Account object if given username == object's username
        }
        return null;
    }
}

我对此进行了评论,因此,如果您不了解 Java ,但Java OOP与PHP OOP相似,就可以了.

I commented it so it will make sense if you don't understand Java but Java OOP is similar to PHP OOP.

从上面的 Java 代码中,我可以将所有对象保存在一个数组中,因此它不会一直查询数据库.

From the Java code above I can hold all objects on an Array, So it doesn't query the Database all the time.

1)我想知道是否可以使用 PHP 做类似的事情来加快代码和缓存类的速度.如果可以的话,请您举个例子.如果不是,那么实现这一目标的最佳方法是什么?

1) I'm wondering if I can do something similar with PHP to speed up code and cache classes. If this is possible can you please show me an example. If not what would be the best way to achieve this?

2),当进行面向对象的编程以保持较低的内存使用量时,可以使用哪些良好的做法?

2) What are some good practices to use when object oriented programming to keep memory usage low?

预先感谢

推荐答案

Java应用程序和PHP应用程序之间的最大区别在于,Java通常是一个持续运行的程序,可同时处理多个传入连接,同时启动PHP实例并对于每个单个请求,托管Web服务器都将其拆除.这意味着您加载的任何类,实例化的对象或分配的变量仅具有毫秒的生存期(在相当快的应用程序中).静态类属性的工作方式与Java中相同,但是,整个应用程序在几毫秒后就会被拆除,因此不能用作长期缓存.更重要的是,每个单独的HTTP请求都是其自己的独立线程,因此在一个线程中分配某些内容不会使其在任何其他同时或后续线程中可见.

The biggest difference between a Java app and a PHP app is that Java is typically a constantly running program handling several incoming connections at once, while PHP instances are started and torn down by the hosting web server for every single individual request. That means any class you load or object you instantiate or variable you assign only has a lifetime of a number of milliseconds (in a decently fast application). Static class properties work the same as in Java, however, the entire application is torn down after a few milliseconds, so this doesn't work as a long-term cache. And more importantly, each individual HTTP request is its own independent thread, so assigning something in one thread doesn't make it visible in any other simultaneous or subsequent threads.

要缓存交叉请求的内容,您需要一个外部数据存储.为此,您可以根据自己的最佳套件有很多选择:

To cache something cross-request, you need an external data store. For this you have many options, depending on what suites you best:

  • serialize to a file
  • APC
  • memcached
  • Redis
  • sessions (for per-user data storage)
  • many more

这篇关于PHP-静态类和缓存类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆