This study proposes a novel fast learning optimiser (FLO) for real-time optimal energy management (OEM) of a grid-connected microgrid. To reduce the optimisation difficulty, the non-convex real-time OEM is decomposed into a two-layer optimisation. The non-convex top-layer optimisation is responsible to determine the direction of tie-line power, and the heat energy outputs of combined heat and power units. Then bottom-layer optimisation is strictly convex with the rest controllable variables, which is solved by the classical interior point method. The model-free Q-learning is employed for knowledge learning and decision making in the top-layer optimisation, thus the feedback reward from the bottom-layer optimisation can effectively realise a coordination between them. The real-coded associative memory is presented for a more efficient optimisation of continuous controllable variables. In order to dramatically reduce the execution time, the knowledge transfer is adopted for approximating the optimal knowledge matrices of a real-time new task by abstracting the optimal knowledge matrices of the predictive source tasks. Simulation results demonstrates that the proposed FLO can rapidly search a high-quality optimum of real-time OEM, in which the computation rate is about 2.75-29.23 times faster than that of eight classical heuristic algorithms.